arne.stockmans
03/05/2019, 3:35 PMjava.lang.ClassCastException: be.gekrabbel.ktortest.MyCustomObject cannot be cast to io.ktor.client.call.HttpClientCall
at io.ktor.client.HttpClient.execute(HttpClient.kt:151)
at io.ktor.client.call.HttpClientCallKt.call(HttpClientCall.kt:80)
at be.gekrabbel.ktortest.MainActivity.callDoesntWork(MainActivity.kt:63)
at be.gekrabbel.ktortest.MainActivity$onCreate$1.invokeSuspend(MainActivity.kt:41)
The strange thing is that when I use a String as body, everything works.
My code:
private val httpClient: HttpClient by lazy {
HttpClient(OkHttp) {
install(JsonFeature) {
serializer = GsonSerializer()
}
}
}
private suspend fun callDoesntWork() {
val call: HttpClientCall = httpClient.call {
method = <http://HttpMethod.Post|HttpMethod.Post>
url(Url("<https://google.com>"))
body = MyCustomObject(message = "Hello World")
}
val response: String = try {
call.response.receive()
} catch (e: BadResponseStatusException) {
"Error ${e.statusCode.value}"
}
println(response)
}
The full source code in a sample project: https://github.com/Arne517/ktor-test/blob/master/app/src/main/java/be/gekrabbel/ktortest/MainActivity.kte5l
03/06/2019, 7:15 AMarne.stockmans
03/06/2019, 8:57 AMe5l
03/06/2019, 8:58 AMcontentType(ContentType.Application.Json)
arne.stockmans
03/06/2019, 9:38 AM