英文:
io.ktor.client.call.NoTransformationFound:class io.ktor.utils.io.ByteBufferChannel(Kotlin reflection is not available)
问题
I'm using Ktor in my KMM project
When I try to parse the JSON response from the API, I always get this exception, but if I make the response return a String, it works.
Here is the code of the method:
suspend fun getSkoolURL22(code: String, token: String): GetSchoolResponse {
return clientv.submitForm(
url = "$CONST_SCHOOL_VERIFICATION_URL.",
formParameters = Parameters.build {
append("token", token)
append("skool_code", code)
}
).body()
}
This is the client:
val clientv = HttpClient() {
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.ALL
}
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
}
And this is my data class:
@Serializable
data class GetSchoolResponse(
@Serializable
val code: Int,
@Serializable
val data: List<Data>,
@Serializable
val message: String
) {
@Serializable
data class Data(
@Serializable
val domain: String,
@Serializable
val id: Int,
@Serializable
val ip: String,
@Serializable
val sk_code: Int,
@Serializable
val sk_name: String
)
}
I got this exception:
io.ktor.client.call.NoTransformationFoundException: No transformation found: class io.ktor.utils.io.ByteBufferChannel (Kotlin reflection is not available) -> class com.example.skoolkmm.data.GetSchoolResponse (Kotlin reflection is not available)
status: 200 OK
response headers:
content-type: text/html; charset=UTF-8
, date: Sun, 11 Jun 2023 21:21:11 GMT
, server: Apache
, transfer-encoding: chunked
, vary: Accept-Encoding
, x-iplb-instance: 38231
, x-iplb-request-id: C4B34E62:51E4_36245B3E:0050_64863AC7_0DE2:25792
, x-powered-by: PHP/7.0
This is my server response:
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> content-type: text/html; charset=UTF-8
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> date: Sun, 11 Jun 2023 21:21:11 GMT
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> server: Apache
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> transfer-encoding: chunked
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> vary: Accept-Encoding
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> x-iplb-instance: 38231
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> x-iplb-request-id: C4B34E62:51E4_36245B3E:0050_64863AC7_0DE2:25792
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> x-powered-by: PHP/7.0
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: BODY Content-Type: text/html; charset=UTF-8
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: BODY START
2023-06-11 22:21:10.230 19455-19511/com.example.skoolkmm.android I/System.out: {"code":200,"message":"success","data":[{"id":1,"domain":"https:\/\/dev.vyzyoschool.com\/","ip":"197.13.17.67","sk_code":111111,"sk_name":"VYZYO"}]}
2023-06-11 22:21:10.230 19455-19511/com.example.skoolkmm.android I/System.out: BODY END
英文:
I'm using Ktor in my KMM project
When i try to parse the json response from api always i get this exception but if i make the response return String it work.
Here is code of method:
suspend fun getSkoolURL22(code:String, token:String): GetSchoolResponse {
return clientv.submitForm(
url = "$CONST_SCHOOL_VERIFICATION_URL.",
formParameters = Parameters.build {
append("token", token)
append("skool_code", code)
}).body() }
This is the client:
val clientv = HttpClient() {
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.ALL }
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
})
}
And this my data Class:
@Serializable
data class GetSchoolResponse(
@Serializable
val code: Int,
@Serializable
val data: List<Data>,
@Serializable
val message: String) {
@Serializable
data class Data(
@Serializable
val domain: String,
@Serializable
val id: Int,
@Serializable
val ip: String,
@Serializable
val sk_code: Int,
@Serializable
val sk_name: String)
}
I got this exception:
io.ktor.client.call.NoTransformationFoundException: No transformation found: class io.ktor.utils.io.ByteBufferChannel (Kotlin reflection is not available) -> class com.example.skoolkmm.data.GetSchoolResponse (Kotlin reflection is not available)
status: 200 OK
response headers:
content-type: text/html; charset=UTF-8
, date: Sun, 11 Jun 2023 21:21:11 GMT
, server: Apache
, transfer-encoding: chunked
, vary: Accept-Encoding
, x-iplb-instance: 38231
, x-iplb-request-id: C4B34E62:51E4_36245B3E:0050_64863AC7_0DE2:25792
, x-powered-by: PHP/7.0
This is my server Response
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> content-type: text/html; charset=UTF-8
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> date: Sun, 11 Jun 2023 21:21:11 GMT
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> server: Apache
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> transfer-encoding: chunked
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> vary: Accept-Encoding
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> x-iplb-instance: 38231
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> x-iplb-request-id: C4B34E62:51E4_36245B3E:0050_64863AC7_0DE2:25792
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: -> x-powered-by: PHP/7.0
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: BODY Content-Type: text/html; charset=UTF-8
2023-06-11 22:21:10.229 19455-19511/com.example.skoolkmm.android I/System.out: BODY START
2023-06-11 22:21:10.230 19455-19511/com.example.skoolkmm.android I/System.out: {"code":200,"message":"success","data":[{"id":1,"domain":"https://dev.vyzyoschool.com/","ip":"197.13.17.67","sk_code":111111,"sk_name":"VYZYO"}]}
2023-06-11 22:21:10.230 19455-19511/com.example.skoolkmm.android I/System.out: BODY END
答案1
得分: 1
由于服务器回复的Content-Type: text/html
和JSON数据,您可以为此特定内容类型注册一个反序列化器以解决该问题:
install(ContentNegotiation) {
// ...
register(
ContentType.Text.Html, KotlinxSerializationConverter(
Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
}
)
)
}
英文:
Since the server replies with the Content-Type: text/html
and the JSON data, you can register a deserializer for this particular content type to solve the problem:
install(ContentNegotiation) {
// ...
register(
ContentType.Text.Html, KotlinxSerializationConverter(
Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
}
)
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论