Retrofit : java.lang.IllegalStateException: closed

6 浏览
0 Comments

Retrofit : java.lang.IllegalStateException: closed

我正在使用两种拦截器,一种是HttpLoggingInterceptor,另一种是我自定义的AuthorizationInterceptor

我正在使用更新的retrofit版本库,如下所示:

def retrofit_version = "2.7.2"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'
implementation 'com.squareup.okhttp3:okhttp:4.4.0'

以下是代码:

private fun makeOkHttpClient(): OkHttpClient {

val logger = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)

return OkHttpClient.Builder()

.addInterceptor(AuthorizationInterceptor(context)) <---- 放置授权屏障

.addInterceptor(logger) <---- 记录Http请求和响应

.followRedirects(false)

.connectTimeout(50, TimeUnit.SECONDS)

.readTimeout(50, TimeUnit.SECONDS)

.writeTimeout(50, TimeUnit.SECONDS)

.build()

}

当我尝试执行下面的代码时,在名为SynchronizationManager.kt的文件中,它给了我一个错误。

var rulesResourcesServices = RetrofitInstance(context).buildService(RulesResourcesServices::class.java)

val response = rulesResourcesServices.getConfigFile(file).execute() <---在这一行我遇到了一个异常...(位于SynchronizationManager.kt:185)

我的RulesResourcesServices类在这里

经过调试后,我发现当调用下面的函数时,此时我遇到了一个异常

@GET("users/me/configfile")

fun getConfigFile(@Query("type") type: String): Call

我得到了以下错误

java.lang.IllegalStateException: closed

at okio.RealBufferedSource.read(RealBufferedSource.kt:184)

at okio.ForwardingSource.read(ForwardingSource.kt:29)

at retrofit2.OkHttpCall$ExceptionCatchingResponseBody$1.read(OkHttpCall.java:288)

at okio.RealBufferedSource.readAll(RealBufferedSource.kt:293)

at retrofit2.Utils.buffer(Utils.java:316)<------- ANDROID IS HIGH-LIGHTING

at retrofit2.BuiltInConverters$BufferingResponseBodyConverter.convert(BuiltInConverters.java:103)

at retrofit2.BuiltInConverters$BufferingResponseBodyConverter.convert(BuiltInConverters.java:96)

at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)

at retrofit2.OkHttpCall.execute(OkHttpCall.java:188)

at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall.execute(DefaultCallAdapterFactory.java:97)

at android.onetap.SynchronizationManager.downloadFile(SynchronizationManager.kt:185)

at android.base.repository.LoginRepository.downloadConfigFilesAndLocalLogin(LoginRepository.kt:349)

at android.base.repository.LoginRepository.access$downloadConfigFilesAndLocalLogin(LoginRepository.kt:48)

at android.base.repository.LoginRepository$loginTask$2.onSRPLoginComplete(LoginRepository.kt:210)

at android.base.repository.LoginRepository$performSyncLogin$srpLogin$1$1.onSRPLogin(LoginRepository.kt:478)

at android.srp.SRPManager$SRPLoginOperation$execute$1.invokeSuspend(SRPManager.kt:323)

at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)

at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)

at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:561)

at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:727)

at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:667)

at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:655)

以下是截图,在截图中,可以看到我得到了文件的输出,但不知道为什么会抛出异常。

enter image description here

检查了Retrofit的Utils类

https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Utils.java

static ResponseBody buffer(final ResponseBody body) throws IOException {
    Buffer buffer = new Buffer();
    body.source().readAll(buffer); <-此行引发错误。
    return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
  }

更新

使用enqueue方法可以正常工作。

response.enqueue(object : Callback {

override fun onResponse(call: Call, response: retrofit2.Response) {

}

})

我已经向Retrofit团队发布了相同的问题,让我们等着看。

https://github.com/square/retrofit/issues/3336

0