Search in sources :

Example 21 with CacheControl

use of okhttp3.CacheControl in project fresco by facebook.

the class OkHttpNetworkFetcher method fetch.

@Override
public void fetch(final OkHttpNetworkFetchState fetchState, final Callback callback) {
    fetchState.submitTime = SystemClock.elapsedRealtime();
    final Uri uri = fetchState.getUri();
    try {
        Request request = new Request.Builder().cacheControl(new CacheControl.Builder().noStore().build()).url(uri.toString()).get().build();
        fetchWithRequest(fetchState, callback, request);
    } catch (Exception e) {
        // handle error while creating the request
        callback.onFailure(e);
    }
}
Also used : Request(okhttp3.Request) Uri(android.net.Uri) IOException(java.io.IOException)

Example 22 with CacheControl

use of okhttp3.CacheControl in project picasso by square.

the class NetworkRequestHandler method createRequest.

private static okhttp3.Request createRequest(Request request, int networkPolicy) {
    CacheControl cacheControl = null;
    if (networkPolicy != 0) {
        if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
            cacheControl = CacheControl.FORCE_CACHE;
        } else {
            CacheControl.Builder builder = new CacheControl.Builder();
            if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
                builder.noCache();
            }
            if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
                builder.noStore();
            }
            cacheControl = builder.build();
        }
    }
    okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(request.uri.toString());
    if (cacheControl != null) {
        builder.cacheControl(cacheControl);
    }
    return builder.build();
}
Also used : CacheControl(okhttp3.CacheControl)

Example 23 with CacheControl

use of okhttp3.CacheControl in project Gradle-demo by Arisono.

the class CacheInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    switch(cacheType) {
        case CacheType.ONLY_NETWORK:
            OkhttpUtils.println("强制使用网络");
            request = request.newBuilder().cacheControl(//只访问網絡
            CacheControl.FORCE_NETWORK).build();
            break;
        case CacheType.ONLY_CACHED:
            OkhttpUtils.println("强制使用缓存");
            request = request.newBuilder().cacheControl(//只访问缓存
            CacheControl.FORCE_CACHE).build();
            break;
        default:
            break;
    }
    Response response = chain.proceed(request);
    Response response1 = response.newBuilder().removeHeader("Pragma").removeHeader("Cache-Control").header("Cache-Control", "max-age=" + cacheTime).build();
    return response1;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

Example 24 with CacheControl

use of okhttp3.CacheControl in project DevRing by LJYcoder.

the class HttpCacheInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    // 对request的设置是用来指定有网/无网下所走的方式
    // 对response的设置是用来指定有网/无网下的缓存时长
    Request request = chain.request();
    if (!NetworkUtil.isNetWorkAvailable(mContext)) {
        // 无网络下强制使用缓存,无论缓存是否过期,此时该请求实际上不会被发送出去。
        // 有网络时则根据缓存时长来决定是否发出请求
        request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
    }
    Response response = chain.proceed(request);
    if (NetworkUtil.isNetWorkAvailable(mContext)) {
        // 有网络情况下,根据请求接口的设置,配置缓存。
        // String cacheControl = request.cacheControl().toString();
        // 有网络情况下,超过1分钟,则重新请求,否则直接使用缓存数据
        // 缓存一分钟
        int maxAge = mCacheTimeWithNet > 0 ? mCacheTimeWithNet : 60;
        String cacheControl = "public,max-age=" + maxAge;
        // 将其超时时间maxAge设为0即可
        return response.newBuilder().header("Cache-Control", cacheControl).removeHeader("Pragma").build();
    } else {
        // 无网络时直接取缓存数据,该缓存数据保存1周
        // 1周
        int maxStale = mCacheTimeWithoutNet > 0 ? mCacheTimeWithoutNet : 60 * 60 * 24 * 7 * 1;
        return response.newBuilder().header("Cache-Control", "public,only-if-cached,max-stale=" + maxStale).removeHeader("Pragma").build();
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

Example 25 with CacheControl

use of okhttp3.CacheControl in project EnableHands by LeviWGG.

the class CacheInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    // 网络不可用
    if (!NetworkUtil.isNetworkAvailable(MyApplication.getMyContext())) {
        request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).url(chain.request().url()).build();
    }
    if (NetworkUtil.isNetworkAvailable(MyApplication.getMyContext())) {
        // 有网络时给相应头加上:缓存超时为0小时
        int maxAge = 0;
        request = request.newBuilder().removeHeader("Pragma").header("Cache-Control", "public, max-age=" + maxAge).build();
    } else {
        // 无网络时,在相应头加上:设置缓存超时为4周
        int maxStale = 60 * 60 * 24 * 28;
        request = request.newBuilder().removeHeader("Pragma").header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).build();
    }
    return chain.proceed(request);
}
Also used : Request(okhttp3.Request)

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