okhttp3 StrictMode Error

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() ,这样就解决了。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_14899485/article/details/52384696
文章标签: okhttp3 StrickMode cache
个人分类: Android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

okhttp3 StrictMode Error

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭