超时后,以下代码
@Throws(ApiException::class)
inline fun <reified R> execute(call: Call<R>): R {
var response: Response<R>? = null
try {
response = call.execute() //line (main.kt:137) THE ONE STACKTRACE POINTS TO!!
} catch (e: IOException) {
//SHOULD close connection
throw ApiException.NetworkError(response?.errorBody()?.string() ?: DEFAULT_REASON, cause = e)
}
if (response != null && response.isSuccessful)
try {
return response.body()!!
} catch (e: Exception) {
response.errorBody()?.close()
}
//SHOULD close connection
throw ApiException.ServiceRespondedNegative(response.errorBody()?.string() ?: DEFAULT_REASON)
}
抛出
java.lang.Throwable: response.body().close()
at okhttp3.internal.platform.Platform.getStackTraceForCloseable(Platform.java:148)
at okhttp3.RealCall.captureCallStackTrace(RealCall.java:89)
at okhttp3.RealCall.execute(RealCall.java:73)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at com.example.client.functional.Api$analyze$1$doResume$$inlined$get$1$1.invoke(main.kt:137)
at com.example.client.functional.MainKt$runInAsyncContext$1$1.doResume(main.kt:97)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:150)
at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:14)
at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
我尝试了try
/ catch
/ finally
的许多组合
stacktrace指向execute()
处的(main.kt:137)
方法调用
不知道如何以及在何处闭合身体。
各地都曾尝试致电raw().close()
,string()
,
还是一样的错误。
答案 0 :(得分:1)
答案是
@Throws(ApiException::class)
inline fun <reified R> execute(call: Call<R>): R {
var response: Response<R>? = null
try {
response = call.execute() //line (main.kt:137) THE ONE STACKTRACE POINTS TO!!
} catch (e: IOException) {
//SHOULD close connection
throw ApiException.NetworkError(response?.errorBody()?.string() ?: DEFAULT_REASON, cause = e)
}
if (response != null && response.isSuccessful)
try {
return response.body()!!
} catch (e: Exception) {
response.errorBody()?.close()
}
else response.close() // something like this line was forgotten.
//SHOULD close connection
//throw ApiException.ServiceRespondedNegative(response.errorBody()?.string() ?: DEFAULT_REASON)
}