Search in sources :

Example 31 with CacheControl

use of okhttp3.CacheControl in project toshi-android-client by toshiapp.

the class OfflineCacheInterceptor method intercept.

@Override
public Response intercept(final Chain chain) throws IOException, IllegalStateException {
    Request request = chain.request();
    if (request.header(CACHE_CONTROL) != null) {
        // Don't override explicitly set cache-control headers
        return chain.proceed(request);
    }
    CacheControl cacheControl = new CacheControl.Builder().maxAge(60, TimeUnit.SECONDS).build();
    if (!BaseApplication.get().isConnected()) {
        cacheControl = new CacheControl.Builder().maxAge(14, TimeUnit.DAYS).maxStale(14, TimeUnit.DAYS).build();
    }
    request = request.newBuilder().header(CACHE_CONTROL, cacheControl.toString()).build();
    return chain.proceed(request);
}
Also used : Request(okhttp3.Request) CacheControl(okhttp3.CacheControl)

Example 32 with CacheControl

use of okhttp3.CacheControl in project toshi-android-client by toshiapp.

the class ReadFromCacheInterceptor method intercept.

@Override
public Response intercept(final Chain chain) throws IOException, IllegalStateException {
    final Response response = chain.proceed(chain.request());
    CacheControl cacheControl = new CacheControl.Builder().maxAge(60, TimeUnit.SECONDS).build();
    if (!BaseApplication.get().isConnected()) {
        cacheControl = new CacheControl.Builder().maxAge(14, TimeUnit.DAYS).build();
    }
    return response.newBuilder().header(CACHE_CONTROL, cacheControl.toString()).build();
}
Also used : Response(okhttp3.Response) CacheControl(okhttp3.CacheControl)

Example 33 with CacheControl

use of okhttp3.CacheControl in project AndroidComponent by funnyzhaov.

the class CacheInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    if (!NetUtil.isNetworkAvailable(mContext)) {
        request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
    }
    Response response = chain.proceed(request);
    if (NetUtil.isNetworkAvailable(mContext)) {
        int maxAge = 0;
        // 有网络时,设置缓存时间为0,拉取最新数据
        response.newBuilder().removeHeader("Cache-Control").header("Cache-Control", "public,max-age=" + maxAge).build();
    } else {
        // 无网络时
        int maxstate = 60 * 60 * 24 * 7;
        response.newBuilder().removeHeader("Cache-Control").header("Cache-Control", "public,only-if-cached,max-state=" + maxstate).build();
    }
    return response;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

Example 34 with CacheControl

use of okhttp3.CacheControl in project My-MVP by REBOOTERS.

the class OkHttpCacheActivity method cacheUse.

private void cacheUse() {
    String cacheDir = getExternalCacheDir().getAbsolutePath();
    File mFile = new File(cacheDir, "android_cache");
    final Cache mCache = new Cache(mFile, 10 * ONE_M);
    OkHttpClient mClient = new OkHttpClient.Builder().readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).retryOnConnectionFailure(true).connectTimeout(10, TimeUnit.SECONDS).addNetworkInterceptor(new CacheInterceptor()).cache(mCache).build();
    Request mRequest = new Request.Builder().url(BASE_URL).build();
    final Call mCall = mClient.newCall(mRequest);
    mCall.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(TAG, "onFailure: e==" + e.toString());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.e(TAG, "onResponse: body==" + response.body().string());
            Log.e(TAG, "onResponse: netResponse==" + response.networkResponse());
            Log.e(TAG, "onResponse: cacheResponse==" + response.cacheResponse());
            Log.e(TAG, "onResponse: cacheControl==" + response.cacheControl().toString());
            Log.e(TAG, "onResponse: threadName==" + Thread.currentThread().getName());
        }
    });
    Call mCall1 = mClient.newCall(mRequest);
    mCall1.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(TAG, "onFailure: e==" + e.toString());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.e(TAG, "onResponse1: body==" + response.body().string());
            Log.e(TAG, "onResponse1: netResponse==" + response.networkResponse());
            Log.e(TAG, "onResponse1: cacheResponse==" + response.cacheResponse());
            Log.e(TAG, "onResponse1: cacheControl==" + response.cacheControl().toString());
            Log.e(TAG, "onResponse1: threadName==" + Thread.currentThread().getName());
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) File(java.io.File) Cache(okhttp3.Cache)

Example 35 with CacheControl

use of okhttp3.CacheControl in project apps-android-wikipedia by wikimedia.

the class CacheControlRequestInterceptor method intercept.

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
    if (!Prefs.preferOfflineContent() || chain.request().cacheControl().noCache()) {
        Request req = chain.request().newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
        Response rsp = null;
        try {
            rsp = chain.proceed(req);
        } catch (IOException ignore) {
        }
        if (rsp != null && rsp.isSuccessful()) {
            return rsp;
        }
    }
    return chain.proceed(chain.request());
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException)

Aggregations

Request (okhttp3.Request)33 Response (okhttp3.Response)28 CacheControl (okhttp3.CacheControl)15 IOException (java.io.IOException)11 File (java.io.File)10 Interceptor (okhttp3.Interceptor)9 Cache (okhttp3.Cache)8 OkHttpClient (okhttp3.OkHttpClient)6 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)6 Provides (dagger.Provides)4 Singleton (javax.inject.Singleton)4 Call (okhttp3.Call)4 RequestBody (okhttp3.RequestBody)4 Uri (android.net.Uri)3 Map (java.util.Map)3 AsyncTask (android.os.AsyncTask)2 ContentLengthInputStream (com.bumptech.glide.util.ContentLengthInputStream)2 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)2 ProgressRequestBody (io.chelizi.amokhttp.upload.ProgressRequestBody)2 FileOutputStream (java.io.FileOutputStream)2