2016-09-09 2 views
1

У меня есть следующий код в моем приложении для Android, работающем в строгом режиме. Я вижу следующее исключение. Я закрываю элемент ответа, возвращенное кодом ниже, вызывая response.close() FWIW Я использую Окио 1.10 и okhttp3 версии 3.4.1Okio явный метод прекращения закрытия не называется

я использую okhttp3 API неправильно или проблема действительно находится в Окио?

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; 

    } 

} 

ответ

0

Исключение состоит в том, что файл кэша не закрывается.

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(); 
     } 
    } 
}