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);
}
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();
}
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;
}
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());
}
});
}
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());
}
Aggregations