我的 Android 应用使用 Jsoup 连接到网站
implementation group: 'org.jsoup', name: 'jsoup', version: '1.11.2'
我的 logcat 有这些消息
OkHttpClient: A connection to http://www.xxx.yyy.zzz/ was leaked. Did you forget to close a response body?
我的 jsoup 连接类似于:-
final Document feedNavigator = Jsoup.connect(uri.toString()).referrer(JSOUP_REFERRER).userAgent(USER_AGENT).followRedirects(true).ignoreContentType(true).ignoreHttpErrors(true).maxBodySize(JSOUP_MAX_BODY_SIZE).timeout(JSOUP_TIMEOUT).get();
我的印象是 jsoup 处理自己的资源。
为什么我会收到这些 logcat 警告?
我该如何解决这个泄漏?
最佳答案
在 Kotlin 我用这种方式:
try {
val req: Request = Request.Builder().url(url).get().build()
val client = OkHttpClient()
val resp: Response = client.newCall(req).execute()
val code: Int = resp.code() // can be any value
body= resp.body()
if (code == 200) {
body?.close() // I close it explicitly
}
关于android - 如何停止 JSoup/OkHttpClient : A connection was leaked?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812145/