Search in sources :

Example 11 with Cache

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;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cache(com.squareup.okhttp.Cache) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache)

Example 12 with Cache

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()));
}
Also used : ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) Cache(com.squareup.okhttp.Cache)

Aggregations

Cache (com.squareup.okhttp.Cache)12 OkHttpClient (com.squareup.okhttp.OkHttpClient)8 File (java.io.File)8 IOException (java.io.IOException)4 Request (com.squareup.okhttp.Request)3 Response (com.squareup.okhttp.Response)2 DiskLruCache (com.squareup.okhttp.internal.DiskLruCache)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 RequestInterceptor (retrofit.RequestInterceptor)2 RestAdapter (retrofit.RestAdapter)2 OkClient (retrofit.client.OkClient)2 StethoInterceptor (com.facebook.stetho.okhttp.StethoInterceptor)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 ApiEndpoint (com.jakewharton.u2020.data.ApiEndpoint)1 PreferencesUtility (com.naman14.timber.utils.PreferencesUtility)1 OkUrlFactory (com.squareup.okhttp.OkUrlFactory)1 Provides (dagger.Provides)1 Singleton (javax.inject.Singleton)1 OkHttpConnector (org.kohsuke.github.extras.OkHttpConnector)1