Search in sources :

Example 6 with Cache

use of com.squareup.okhttp.Cache in project remusic by aa112901.

the class HttpUtil method getResposeJsonObject.

public static JsonObject getResposeJsonObject(String action1, Context context, boolean forceCache) {
    try {
        Log.e("action-cache", action1);
        File sdcache = context.getCacheDir();
        //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(action1);
        if (forceCache) {
            builder.cacheControl(CacheControl.FORCE_CACHE);
        }
        Request request = builder.build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String c = response.body().string();
            Log.e("cache", c);
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(c);
            return el.getAsJsonObject();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) JsonElement(com.google.gson.JsonElement) 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) JsonParser(com.google.gson.JsonParser)

Example 7 with Cache

use of com.squareup.okhttp.Cache in project Rutgers-Course-Tracker by tevjef.

the class RutgersCTModule method providesOkHttpClient.

@Provides
@Singleton
public OkHttpClient providesOkHttpClient(Context context) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.networkInterceptors().add(new StethoInterceptor());
    File httpCacheDir = new File(context.getCacheDir(), context.getString(R.string.application_name));
    // 50 MiB
    long httpCacheSize = 50 * 1024 * 1024;
    Cache cache = new Cache(httpCacheDir, httpCacheSize);
    client.setCache(cache);
    if (BuildConfig.DEBUG) {
        try {
            cache.evictAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return client;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp.StethoInterceptor) IOException(java.io.IOException) File(java.io.File) Cache(com.squareup.okhttp.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

Cache (com.squareup.okhttp.Cache)7 OkHttpClient (com.squareup.okhttp.OkHttpClient)5 File (java.io.File)5 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 Provides (dagger.Provides)1 Singleton (javax.inject.Singleton)1 Retrofit (retrofit.Retrofit)1