简体   繁体   中英

Kotlinx-serialization-json: java.util.NoSuchElementException: List is empty

I get error "java.util.NoSuchElementException: List is empty." from Kotlinx-serialization-json cause the body`s contentLenght is -1. But it is 44 bytes if check in interceptor. I guess this property (contentLenght in RealResponseBody) should be forced to 44 in interceptor. Or force Kotlinx-serialization-json to ignore this property when serializing.

I use Retrofit, RxJava and Serialization:

// RxJava
implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.21"
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
implementation "com.jakewharton.rxrelay2:rxrelay:2.1.1"

// Retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"

// Serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"

My interceptor:

override fun intercept(chain: Interceptor.Chain): Response {
    val originalResponse = chain.proceed(chain.request())
    val builder = originalResponse.newBuilder()
        .addHeader("Content-Length", (originalResponse.body?.bytes()?.size ?: 0).toString())
        .build()

    return builder
}

My Response:


2022-03-20 11:54:04.936 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: --> POST https://test.hide/api/v4/registration/sign_in/
2022-03-20 11:54:04.936 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Content-Type: application/json; charset=utf-8
2022-03-20 11:54:04.936 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Content-Length: 281
2022-03-20 11:54:04.937 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: {"number":"123123123","last_name":"Test","email":"index@gmail.com","fcm_token":"fxktgapBQVe3pOior0dUJ-:APA91bGPpa_uFfurGg7mKurOHHdEB7ipZHG_KkBrE2V5F476ErB7FcEQorG5-X6BTXC3jrtmVKItYvVxrTyWMILXxdw-1w8kYNRgDtL54f5uBNda_4gYV5Ij8oaC1prHyu9x_uSZ4zsw","device_os":"Android"}
2022-03-20 11:54:04.937 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: --> END POST (281-byte body)
2022-03-20 11:54:04.940 6536-6604/com.example.app2_kotlin D/test: Here is cookie https://test.hide/api/v4/registration/sign_in/
2022-03-20 11:54:05.435 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: <-- 200 OK https://test.hide/api/v4/registration/sign_in/ (498ms)
2022-03-20 11:54:05.435 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Server: nginx/1.11.13
2022-03-20 11:54:05.435 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Date: Sun, 20 Mar 2022 08:54:06 GMT
2022-03-20 11:54:05.435 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Content-Type: application/json
2022-03-20 11:54:05.435 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Transfer-Encoding: chunked
2022-03-20 11:54:05.436 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Connection: keep-alive
2022-03-20 11:54:05.436 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Allow: POST, OPTIONS
2022-03-20 11:54:05.436 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: Vary: Origin
2022-03-20 11:54:05.437 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: {"message": "User successfully registered."}
2022-03-20 11:54:05.437 6536-6604/com.example.app2_kotlin I/okhttp.OkHttpClient: <-- END HTTP (44-byte body)

The problem was in "Tranfer-Encoding" header. It was "gzip". Using this header tell the server that client is receiving packed data in response. The server in this case begins to return data in a "split" form.

So the solution is not to use this header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM