Android 改装2和kotlin:未反序列化问题:IllegelArgumentException:已关闭

Android 改装2和kotlin:未反序列化问题:IllegelArgumentException:已关闭,android,kotlin,gson,retrofit2,okhttp,Android,Kotlin,Gson,Retrofit2,Okhttp,嗨,我是新来改装的,我正在使用 implementation 'com.squareup.retrofit2:retrofit:2.6.1' // JSON Parsing implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' 我正在进行登录的API调用。调用是成功的(我可以在我添加的拦截器中看到200响应),但我仍然在使用jav

嗨,我是新来改装的,我正在使用

implementation 'com.squareup.retrofit2:retrofit:2.6.1'
// JSON Parsing
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
我正在进行登录的API调用。调用是成功的(我可以在我添加的拦截器中看到200响应),但我仍然在使用
java.lang.IllegalStateException:closed
作为throwable进行改型时收到失败回调。我在改型类中添加了一些断点,它似乎是在解析我的响应时出现的。我也在使用Kotlin

这是我的数据类:

data class LoginResponse(
    @SerializedName("payload") @Expose val payload: Payload,
    @SerializedName("status") @Expose val status: Int
)
data class Payload(
    @SerializedName("access_token") @Expose val access_token: String?,
    @SerializedName("expires_at") @Expose val expires_at: String?,
    @SerializedName("name") @Expose val name: String?,
    @SerializedName("org_name") @Expose val org_name: String?,
    @SerializedName("request_at") @Expose val request_at: String,
    @SerializedName("status_message") @Expose val status_message: String
)
下面是我创建改装实例的方式:

private val okHttpClient = OkHttpClient.Builder().addInterceptor(Interceptor {
    Log.w("Retrofit@Request", it.request().toString())
    val requestBuilder = it.request().newBuilder()
    requestBuilder.header("Content-Type", "application/json")
    val response = it.proceed(it.request())
    Log.w("Retrofit@Response", response.body()!!.string())  //correct 200 response with json is printed out here
    return@Interceptor response
}).build()

val retrofitInstance: Retrofit
    get() {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }
        return retrofit!!
    }
以下是我打电话的方式:

loginService.login(logindata).enqueue(object : Callback<LoginResponse> {
                override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
                    Log.d(TAG, "Login failure "+t) //this is getting invoked with java.lang.IllegalStateException: closed
                }

                override fun onResponse(
                    call: Call<LoginResponse>,
                    response: Response<LoginResponse>
                ) {
                    onLoginState.value = true
                }
            })
loginService.login(loginda).enqueue(对象:回调){
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(标记,“登录失败”+t)//这是通过java.lang.IllegalStateException调用的:closed
}
覆盖响应(
呼叫:呼叫,,
答复:答复
) {
onLoginState.value=true
}
})
我花了很多时间试图弄清楚发生了什么,却找不到我做错了什么。我还尝试用Moshi替换GSON,这给了我同样的错误。请帮我解决这个问题


提前谢谢。

我不知道这是否有帮助,但请尝试阅读并删除您的日志
log.w(“Retrofit@Response,response.body()!!.string())
或替换为中的提示

原因: 身体的字符串存储在变量内存中,所以我们不需要关心状态是否被关闭。

,简而言之,你只能调用<代码> String()/<代码>,并且你已经在拦截器上做了。也考虑使用。
// Don't try to use response.body.string() directly. Just using like below:
ResponseBody responseBody = response.body();
String content = responseBody.string();
// Do something with "content" variable