我一直收到这个错误:
A connection to ****** was leaked. Did you forget to close a response body?
所以我继续并关闭我收到的回复。
response.body().close()
问题是如果 response.body() 已经转换为自定义类,则没有可用的关闭方法。我也尝试调用 raw 并给了我一个异常(exception):
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) {
}
});
}
如何关闭它?
最佳答案
如前所述here你可以做如下的事情
ResponseBody body = response.raw().body();
if (response.isSuccessful()) {
return body.string(); // Closes automatically.
} else {
body.close();
return null;
}
或
ResponseBody body = response.raw().body();
try {
...
} finally {
body.close();
}
希望它能解决您的问题。
关于android - 改造关闭响应机构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39482514/