我使用 Jsoup 从远程 URL 解析文件,如下所示:
Document doc = Jsoup.connect(urlString)
.timeout(5000)
.get();
如果网站未能在超时内响应,SocketTimeoutException
则如预期的那样抛出 a。我还打开了 StrictMode 以警告我有关未关闭的资源。在超时时,StrictMode 告诉我资源没有关闭:
A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at java.io.FileInputStream.<init>(FileInputStream.java:80)
at libcore.io.DiskLruCache.get(DiskLruCache.java:391)
at libcore.net.http.HttpResponseCache.get(HttpResponseCache.java:98)
at android.net.http.HttpResponseCache.get(HttpResponseCache.java:204)
at libcore.net.http.HttpEngine.initResponseSource(HttpEngine.java:257)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:218)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
at org.jsoup.helper.HttpConnection$Response.a(SourceFile:439)
at org.jsoup.helper.HttpConnection$Response.a(SourceFile:465)
at org.jsoup.helper.HttpConnection$Response.a(SourceFile:424)
at org.jsoup.helper.HttpConnection.execute(SourceFile:178)
at org.jsoup.helper.HttpConnection.get(SourceFile:167)
at com.example.$3.call(SourceFile:163)
at a.e.run(SourceFile:198)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
我看不到如何关闭 Jsoup 创建的资源。我该怎么做?