StrictMode:在附加堆栈跟踪中获取了资源,但从未释放过

时间:2019-01-30 11:27:36

标签: android retrofit2 okhttp okhttp3 android-strictmode

有时我会收到此SctrictMode异常:

2019-01-28 14:18:23.073 4597-4615/my.app.package E/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 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:223)
        at java.util.zip.Inflater.<init>(Inflater.java:106)
        at okio.GzipSource.<init>(GzipSource.java:62)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:103)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:213)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at my.app.package.api.network.SetJsonContentTypeHeaderInterceptor.intercept(SetJsonContentTypeHeaderInterceptor.kt:15)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

异常代码中提到的属于我的应用的唯一部分是SetJsonContentTypeHeaderInterceptor

import okhttp3.Interceptor

internal class SetJsonContentTypeHeaderInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain) =
        chain.request()
            .let {
                it.newBuilder()
                    .header("Content-Type", "application/json")
                    .header("Accept", "application/json")
                    .build()
            }
            .let { chain.proceed(it) }!!

}

应用中使用的库版本:

okHttp:3.11.0

改造:2.4.0

这是什么原因以及如何解决?

0 个答案:

没有答案