Retrofit Closing Response Body
I've been getting this error: A connection to ****** was leaked. Did you forget to close a response body? So I went on and close the responses I get. response.body().close() Pro
Solution 1:
As mentioned here you can do something like below
ResponseBody body = response.raw().body();
if (response.isSuccessful()) {
return body.string(); // Closes automatically.
} else {
body.close();
returnnull;
}
or
ResponseBodybody= response.raw().body();
try {
...
} finally {
body.close();
}
Hope it will solve your issue.
Post a Comment for "Retrofit Closing Response Body"