关闭

okhttp3 StrictMode Error

标签: okhttp3StrickModecache
84人阅读 评论(0) 收藏 举报
分类:

okhttp3 StrictMode Error

使用okhttp3.3过程中遇到以下bug,google了一下也没找到解决办法,现放出自己的解决办法:

E/StrictMode(16877): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
E/StrictMode(16877): java.lang.Throwable: Explicit termination method 'close' not called
E/StrictMode(16877):    at dalvik.system.CloseGuard.open(CloseGuard.java:184)
E/StrictMode(16877):    at java.io.FileOutputStream.<init>(FileOutputStream.java:90)
E/StrictMode(16877):    at okio.Okio.appendingSink(Okio.java:187)
E/StrictMode(16877):    at okhttp3.internal.io.FileSystem$1.appendingSink(FileSystem.java:59)
E/StrictMode(16877):    at okhttp3.internal.DiskLruCache.newJournalWriter(DiskLruCache.java:308)
E/StrictMode(16877):    at okhttp3.internal.DiskLruCache.readJournal(DiskLruCache.java:300)
E/StrictMode(16877):    at okhttp3.internal.DiskLruCache.initialize(DiskLruCache.java:227)
E/StrictMode(16877):    at okhttp3.internal.DiskLruCache.get(DiskLruCache.java:429)
E/StrictMode(16877):    at okhttp3.Cache.get(Cache.java:193)
E/StrictMode(16877):    at okhttp3.Cache$1.get(Cache.java:143)
E/StrictMode(16877):    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:203)
E/StrictMode(16877):    at okhttp3.RealCall.getResponse(RealCall.java:243)
E/StrictMode(16877):    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
E/StrictMode(16877):    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
E/StrictMode(16877):    at okhttp3.RealCall.access$100(RealCall.java:30)
E/StrictMode(16877):    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
E/StrictMode(16877):    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
E/StrictMode(16877):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/StrictMode(16877):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/StrictMode(16877):    at java.lang.Thread.run(Thread.java:841)

我的问题就在于 cache 没有及时 close,解决办法很简单:
在 onResponse 和 onFailure 后都加上 cache.close() ,这样就解决了。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3843次
    • 积分:164
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论