Hi all, I'm learning how to use Ktor Client for us...
# ktor
p
Hi all, I'm learning how to use Ktor Client for use in an Android app using this documentation: https://ktor.io/docs/full-stack-development-with-kotlin-multiplatform.html#create-server (But applying this example for an Android app not KMP also I use Ktor 3.0.0) Now I have a problem when testing from an Android emulator in the steps here: https://ktor.io/docs/full-stack-development-with-kotlin-multiplatform.html#create-client I do succeed in calling the Ktor server when creating a simple test client app using:
Copy code
suspend fun main() {
    val client = HttpClient(CIO)
    val response = client.get("<http://localhost:8080/tasks>")
    println(response.bodyAsText())
}
But from the Android emulator I do not succeed. I tried by changing host to 10.0.2.2.
Copy code
class TaskApi {
    private val httpClient = HttpClient {
        install(ContentNegotiation) {
            json(Json {
                encodeDefaults = true
                isLenient = true
                coerceInputValues = true
                ignoreUnknownKeys = true
            })
        }
        defaultRequest {
            host = "10.0.2.2"
            port = 8080
        }
    }

    suspend fun getAllTasks(): List<Task> {
        return httpClient.get("tasks").body()
    }
...
}
When testing this from an Android emulator I get the error (see snippet). Any clue for this problem?
e
Hey, @Paul at Avans Breda! Could you tell me if you have added the network permission to the AndroidManifest file?
p
Ah, thanks a lot Leonid! It works now. I added android:usesCleartextTraffic="true" but forgot this permission. I missed it when reading the documentation (now I see it is written in a later step in the documentation).