Android 试图进行第二次网络调用时发生OkHttp调度程序非法状态异常
当我尝试将我的网络调用从“com.jakewharton.reformation:Reformation2 kotlin coroutines adapter”迁移到内置的Reformation2.6挂起功能时,我遇到了这个问题 在这个场景中,我正在进行多个连续的网络调用以从API获取数据。除了第二次改装呼叫,我的第一次呼叫成功,没有任何问题。它给了我以下的错误 我在代码中所做的更改Android 试图进行第二次网络调用时发生OkHttp调度程序非法状态异常,android,okhttp,illegalstateexception,kotlin-coroutines,retrofit2.6,Android,Okhttp,Illegalstateexception,Kotlin Coroutines,Retrofit2.6,当我尝试将我的网络调用从“com.jakewharton.reformation:Reformation2 kotlin coroutines adapter”迁移到内置的Reformation2.6挂起功能时,我遇到了这个问题 在这个场景中,我正在进行多个连续的网络调用以从API获取数据。除了第二次改装呼叫,我的第一次呼叫成功,没有任何问题。它给了我以下的错误 我在代码中所做的更改 从app.gradle中删除适配器库并更新我的改装 至2.7 删除了wait()调用执行行,并在改型接口方法中添
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>>
@POST(ServiceTypes.USER\u配置文件)
suspend fun fetchProfile():响应
@POST(服务类型.类别)
suspend fun fetchCategories(@Body categoriesRequest:categoriesRequest):响应
错误在于您的拦截器(com.chilindo.base.network.ChilindoAPIL)在发出另一个请求之前没有关闭响应。这是对旧的OkHttp版本的无声泄漏;今天,它已被检测到,因此您可以避免泄漏。提供一些信息code@SkypeDogg代码已添加,但未看到方法调用。