【发布时间】:2017-01-21 19:00:29
【问题描述】:
我收到了这个错误:
A connection to ****** was leaked. Did you forget to close a response body?
所以我继续关闭收到的回复。
response.body().close()
问题是如果 response.body() 已经转换为自定义类,则没有可用的 close 方法。我也尝试调用 raw 并给了我一个例外:
fetchSomething.enqueue(new Callback<SomethingClass>() {
@Override
public void onResponse(Call<SomethingClass> call, Response<SomethingClass> response) {
//Closes the response body
response.raw().body().close(); //<--- gives illegalStateException
}
@Override
public void onFailure(Call<SomethingClass> call, Throwable t) {
}
});
}
如何关闭它?
【问题讨论】:
-
retrofit和okhttp版本是什么? -
编译 'com.squareup.retrofit2:retrofit:2.0.0-beta4' 编译 'com.squareup.retrofit2:converter-jackson:2.0.0-beta4' 编译 'com.squareup.okhttp3: logging-interceptor:3.2.0' 编译 "com.squareup.okhttp3:okhttp-urlconnection:3.0.1"
-
能否更新
retrofit和okhttp库并再次检查代码。 -
那些是非常旧的版本。更新并重试。 Retrofit 2 为你关闭了 ResponseBody;无需在应用层这样做。
-
这成功了,谢谢@EricCochran
标签: android retrofit retrofit2 okhttp okhttp3