io.ktor.client.call.NoTransformationFound:class io.ktor.utils.io.ByteBufferChannel(Kotlin reflection is not available)

huangapple go评论60阅读模式
英文:

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 = &quot;$CONST_SCHOOL_VERIFICATION_URL.&quot;,
                formParameters = Parameters.build {
                    append(&quot;token&quot;, token)
                    append(&quot;skool_code&quot;, 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&lt;Data&gt;,
    @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) -&gt; 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
            }
        )
    )
}

huangapple
  • 本文由 发表于 2023年6月12日 05:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定