【问题标题】:Okio explicit termination method close not calledOkio 显式终止方法关闭未调用
【发布时间】:2017-01-18 02:24:05
【问题描述】:

我在严格模式下运行的 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)

创建响应对象的代码如下。

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;

    }

}

【问题讨论】:

    标签: android okhttp3 okio


    【解决方案1】:

    例外是因为缓存文件没有被关闭。

    Context context;
    int httpCacheSize = 100 * 1024 * 1024; // 100mb
    File httpCacheDir = new File(context.getCacheDir(), "http");
    Cache cache = new Cache(httpCacheDir, httpCacheSize);
    OkHttpClient client = new OkHttpClient.Builder()
            .cache(cache)
            .readTimeout(WebServicesManager.READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
            .connectTimeout(WebServicesManager.CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
            .build();
    Request request = new Request.Builder()
            .url(downloadUrl)
            .get()
            .build();
    Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (response != null) {
            response.close();
        }
        if (!cache.isClosed()) {
            try {
                cache.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      相关资源
      最近更新 更多