Okio显式终止方法关闭未调用

时间:2016-09-09 21:29:00

标签: android okhttp3 okio

我在严格模式下运行的Android应用中有以下代码。我看到以下异常被抛出。我正在通过调用response.close()来关闭下面代码返回的响应项 fwiw我使用的是okio 1.10和okhttp3版本3.4.1

我是否使用okhttp3 API错误或问题确实在okio中?

    E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.

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:180)
at java.io.FileOutputStream.<init>(FileOutputStream.java:222)
at okio.Okio.appendingSink(Okio.java:185)
at okhttp3.internal.io.FileSystem$1.appendingSink(FileSystem.java:59)
at okhttp3.internal.cache.DiskLruCache.newJournalWriter(DiskLruCache.java:310)
at okhttp3.internal.cache.DiskLruCache.readJournal(DiskLruCache.java:302)
at okhttp3.internal.cache.DiskLruCache.initialize(DiskLruCache.java:229)
at okhttp3.internal.cache.DiskLruCache.get(DiskLruCache.java:431)
at okhttp3.Cache.get(Cache.java:194)
at okhttp3.Cache$1.get(Cache.java:144)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:70)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.execute(RealCall.java:60)

创建Response对象的代码如下。

protected Response getResponse(OkHttpClient client,
                                              String downloadUrl)  {

    Request request = new Request.Builder().
            url(downloadUrl).get().build();

    Call httpRequest = client.newCall(request);

    try {

      return httpRequest.execute();

    }

    catch (Exception e) {

      Timber.e("Error downloading url %s",downloadUrl);

      return null;

    }

}

1 个答案:

答案 0 :(得分:0)

异常是因为缓存文件没有被关闭。

{{1}}