1

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

    }

}
4

1 回答 1

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();
        }
    }
}
于 2016-12-23T20:53:26.817 回答