I am trying to get my android app to call my local...
# ktor
j
I am trying to get my android app to call my local server. When I do this it works fine:
Copy code
curl -X POST -d 'Hello' <http://0.0.0.0:8080/outfit_data>
success%
The error I get from android is:
Copy code
I/System.out: Failed to connect to /0.0.0.0:8080 calling <http://0.0.0.0:8080/outfit_data>
This is my ktor 2.0.0 routing:
Copy code
post("/outfit_data") {
    val data = call.receive<String>()
    println(data)
    call.respondText("success", null, HttpStatusCode.OK)
}
And on the android side this is how I make the call, and baseUrl is set as this is the top-level gradle.properties file
Copy code
weather_client_host_name=<http://0.0.0.0:8080>
Copy code
try {
    <http://client.post|client.post>(
        "$baseUrl/outfit_data"
    ) {
        contentType(ContentType.Application.Json)
        setBody(data)
    }.also { response ->
        return response.status == HttpStatusCode.OK || response.status == HttpStatusCode.Created
    }
} catch(e:Throwable) {
    println("${e.message} calling $baseUrl/outfit_data")
    return false
}
My code is in the androidApp, shared/androidMain and server subprojects. The http call for Android is in shared/src/androidMain/.../api/WeatherApi.kt I also put this in the android manifest in the shared/src/androidMain:
Copy code
<application
    android:usesCleartextTraffic="true">
https://github.com/jblack975/MyOutfitPicker/tree/pass_anon_data_to_server
a
Hello, did you run your server on the PC?
a
The port on your host machine isn’t exposed by default to an Android emulator or a physical device. You need to forward a port via adb or connect through a local wifi network.
c
you cannot use the loopback on an emulator. the development machine is reachable via
10.0.2.2.
see here https://developer.android.com/studio/run/emulator-networking
If you are working on a physical device you need the local IP of your development machine.
j
Thanks, let me try that, as I am just using an emulator atm.