尝试进行第二次网络调用时出现OkHttp Dispatcher IllegalStateException



当我试图将我的网络呼叫从"com.jakeharton.reformit:retrofit2 kotlin coroutines adapter"迁移到内置的reforming 2.6挂起功能时,我遇到了这个问题。

场景中,我正在进行多个连续的网络调用以从API中获取数据。我的第一个电话成功了,没有任何问题,但第二个改装电话。它给了我以下错误。

我在代码中所做的更改

  1. 从app.gradle中删除了适配器库并更新了我的改装至2.7
  2. 删除了await((调用执行行,并在改装接口方法中添加了"suspend"函数
  3. 删除了延迟变量改造接口方法

我的Logcat控制台

2020-02-26 17:08:19.777 E: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.chilindo, PID: 22114
java.lang.IllegalStateException: cannot make a new request because the previous response is still open: please call response.close()
at okhttp3.internal.connection.Transmitter.newExchange$okhttp(Transmitter.kt:157)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:35)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at com.chilindo.base.network.ChilindoAPIL$Companion$invoke$$inlined$-addInterceptor$1.intercept(Interceptor.kt:144)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:219)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:194)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

改造后2.6

override suspend fun fetchUserProfile() {
try {
val result = chilindoAPI.fetchProfile()
if (result.isSuccessful) {
_userProfileTable.postValue(result.body())
}
} catch (e: HttpExceptionExtended) {
e.printStackTrace()
} catch (e: NoConnectivityException) {
e.printStackTrace()
}
}
override suspend fun fetchCategoriesResponse(domain: String, language: String) {
try {
val categoriesRequest = CategoriesRequest()
categoriesRequest.countryName = domain
categoriesRequest.language = language
val result = chilindoAPI.fetchCategories(categoriesRequest)
if (result.isSuccessful) {
_categoriesResponse.postValue(result.body())
}
} catch (e: HttpExceptionExtended) {
e.printStackTrace()
_categoriesResponse.postValue(null)
} catch (e: NoConnectivityException) {
e.printStackTrace()
}
}

和API接口

@POST(ServiceTypes.USER_PROFILE)
suspend fun fetchProfile(): Response<UserProfileTable>
@POST(ServiceTypes.CATEGORIES)
suspend fun fetchCategories(@Body categoriesRequest: CategoriesRequest): Response<List<CategoriesResponse>>

错误在于您的拦截器(com.chilindo.base.network.ChilindoAPIL(在发出另一个请求之前没有关闭响应。这是旧的OkHttp版本的无声泄漏;今天它被检测到了,所以你可以避免泄漏。

最新更新