Hello all. I was wondering if anyone knows about t...
# ktor
m
Hello all. I was wondering if anyone knows about this error:
java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8080
I have a ktor server and an android app running on the same multiplatform project. Google doesn't say anything about the error except for this https://stackoverflow.com/questions/62500669/ktor-android-local-http-server-throws-error. I tried the solution and got nothing
Full stacktrace:
Copy code
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.surrus.peopleinspace, PID: 19275
    java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8080
        at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:147)
        at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at io.ktor.client.engine.android.AndroidClientEngine$execute$2.invoke(AndroidClientEngine.kt:91)
        at io.ktor.client.engine.android.AndroidClientEngine$execute$2.invoke(AndroidClientEngine.kt:90)
        at io.ktor.client.engine.android.AndroidURLConnectionUtilsKt.timeoutAwareConnection(AndroidURLConnectionUtils.kt:57)
        at io.ktor.client.engine.android.AndroidClientEngine.execute(AndroidClientEngine.kt:90)
        at io.ktor.client.engine.HttpClientEngine$executeWithinCallContext$2.invokeSuspend(HttpClientEngine.kt:99)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
    	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@d539a42, androidx.compose.runtime.BroadcastFrameClock@6a4fa53, StandaloneCoroutine{Cancelling}@7b60f90, AndroidUiDispatcher@aa2b089]
W/System: A resource failed to call close.
I got to this point by editing the People in Space multiplatform app. https://github.com/joreilly/PeopleInSpace I edited the Android code and the compose-web to connect to the server.kt. It worked fine on compose-web, but doesn't work on Android. Let me know if you want code samples
a
Is your Ktor server launched in the same Android emulator/device?
m
If you're not actually trying to run ktor on Android itself - remember that localhost on the emulator means the emulator itself! You need to use your laptops IP address (not 127.0.0.1). If you're trying to run ktor on Android itself - then my advice would be to avoid that. It's not really designed to do that, it is intended to run in the JVM on a server. It's dependencies would likely significantly increase the APK size. We have been using NanoHTTPD as an embedded web server (which is tiny - maybe less than 100KB). If you a function you want to work on both Android on the server you can write the core logic in one place and then access it from both Ktor and NanoHTTPD (which I'm doing).
🙌 1
m
Mindblown. Thanks. Yeah I'm running ktor from my laptop! I will try using the laptop ip address
706 Views