Android 未调用Okio显式终止方法close
我的Android应用程序中有以下代码以严格模式运行。我看到以下异常被抛出。我正在通过调用Android 未调用Okio显式终止方法close,android,okhttp3,okio,Android,Okhttp3,Okio,我的Android应用程序中有以下代码以严格模式运行。我看到以下异常被抛出。我正在通过调用response.close() fwiw我正在使用okio 1.10和okhttp3版本3.4.1 我使用OKHTTP3API是错误的还是问题真的存在于okio中 E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for informatio
response.close()
fwiw我正在使用okio 1.10和okhttp3版本3.4.1
我使用OKHTTP3API是错误的还是问题真的存在于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)
异常是因为缓存文件未关闭
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();
}
}
}
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();
}
}
}