How To Solve OKHttp Connection Was Leaked

How To Solve OKHttp Connection Was Leaked

這篇介紹How To Solve OKHttp Connection Was Leaked。

發生情境

1
2
3
使用OKHttp時,若call.execute();失敗時
會出現okhttp3.internal.platform.Platform.log A connection to https://xxx was leaked.
Did you forget to close a response body? 的警告

解決方法

1
2
3
4
5
使用try-catch-finally處理,在finally裡面不管成功或失敗
都必須將response.close();
if (response != null) {
response.close();
}