use of com.squareup.okhttp.Cache in project remusic by aa112901.
the class HttpUtil method getBitmapStream.
public static Bitmap getBitmapStream(Context context, String url, boolean forceCache) {
try {
File sdcache = context.getExternalCacheDir();
// File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
// 30Mb
Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30);
mOkHttpClient.setCache(cache);
mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
Request.Builder builder = new Request.Builder().url(url);
if (forceCache) {
builder.cacheControl(CacheControl.FORCE_CACHE);
}
Request request = builder.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
return _decodeBitmapFromStream(response.body().byteStream(), 160, 160);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.squareup.okhttp.Cache in project sbt-android by scala-android.
the class DebugView method refreshOkHttpCacheStats.
private void refreshOkHttpCacheStats() {
// Shares the cache with apiClient, so no need to check both.
Cache cache = client.getCache();
int writeTotal = cache.getWriteSuccessCount() + cache.getWriteAbortCount();
int percentage = (int) ((1f * cache.getWriteAbortCount() / writeTotal) * 100);
okHttpCacheWriteErrorView.setText(cache.getWriteAbortCount() + " / " + writeTotal + " (" + percentage + "%)");
okHttpCacheRequestCountView.setText(String.valueOf(cache.getRequestCount()));
okHttpCacheNetworkCountView.setText(String.valueOf(cache.getNetworkCount()));
okHttpCacheHitCountView.setText(String.valueOf(cache.getHitCount()));
}
Aggregations