当我尝试将网络调用从“com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter”迁移到内置的改造 2.6 挂起功能时,我遇到了这个问题。
场景,我正在进行多个顺序网络调用以从 API 获取数据。我的第一个电话成功了,没有任何问题,但第二次改造电话。它给了我以下错误。
我在代码中所做的更改,
- 从 app.gradle 中删除了适配器库并将我的改造更新到 2.7。
- 移除了 await() 调用执行行,并在改造接口方法中添加了“挂起”功能。
- 删除了延迟变量改造接口方法。
我的 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>>