use of okhttp3.CacheControl in project AntennaPod by AntennaPod.
the class ItunesTopListLoader method getTopListFeed.
private String getTopListFeed(OkHttpClient client, String country, int limit) throws IOException {
String url = "https://itunes.apple.com/%s/rss/toppodcasts/limit=" + limit + "/explicit=true/json";
Log.d(TAG, "Feed URL " + String.format(url, country));
Request.Builder httpReq = new Request.Builder().cacheControl(new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build()).url(String.format(url, country));
try (Response response = client.newCall(httpReq.build()).execute()) {
if (response.isSuccessful()) {
return response.body().string();
}
if (response.code() == 400) {
throw new IOException("iTunes does not have data for the selected country.");
}
String prefix = context.getString(R.string.error_msg_prefix);
throw new IOException(prefix + response);
}
}
use of okhttp3.CacheControl in project ExoPlayer by google.
the class OkHttpDataSource method makeRequest.
/**
* Establishes a connection.
*/
private Request makeRequest(DataSpec dataSpec) throws HttpDataSourceException {
long position = dataSpec.position;
long length = dataSpec.length;
@Nullable HttpUrl url = HttpUrl.parse(dataSpec.uri.toString());
if (url == null) {
throw new HttpDataSourceException("Malformed URL", dataSpec, PlaybackException.ERROR_CODE_FAILED_RUNTIME_CHECK, HttpDataSourceException.TYPE_OPEN);
}
Request.Builder builder = new Request.Builder().url(url);
if (cacheControl != null) {
builder.cacheControl(cacheControl);
}
Map<String, String> headers = new HashMap<>();
if (defaultRequestProperties != null) {
headers.putAll(defaultRequestProperties.getSnapshot());
}
headers.putAll(requestProperties.getSnapshot());
headers.putAll(dataSpec.httpRequestHeaders);
for (Map.Entry<String, String> header : headers.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
@Nullable String rangeHeader = buildRangeRequestHeader(position, length);
if (rangeHeader != null) {
builder.addHeader(HttpHeaders.RANGE, rangeHeader);
}
if (userAgent != null) {
builder.addHeader(HttpHeaders.USER_AGENT, userAgent);
}
if (!dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP)) {
builder.addHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
}
@Nullable RequestBody requestBody = null;
if (dataSpec.httpBody != null) {
requestBody = RequestBody.create(null, dataSpec.httpBody);
} else if (dataSpec.httpMethod == DataSpec.HTTP_METHOD_POST) {
// OkHttp requires a non-null body for POST requests.
requestBody = RequestBody.create(null, Util.EMPTY_BYTE_ARRAY);
}
builder.method(dataSpec.getHttpMethodString(), requestBody);
return builder.build();
}
use of okhttp3.CacheControl in project picasso by square.
the class NetworkRequestHandler method createRequest.
private static okhttp3.Request createRequest(Request request) {
CacheControl cacheControl = null;
int networkPolicy = request.networkPolicy;
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();
}
}
Uri uri = checkNotNull(request.uri, "request.uri == null");
okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(uri.toString());
if (cacheControl != null) {
builder.cacheControl(cacheControl);
}
return builder.build();
}
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;
}
use of okhttp3.CacheControl in project SeeWeather by xcc3641.
the class RetrofitSingleton method initOkHttp.
private static void initOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 缓存 http://www.jianshu.com/p/93153b34310e
File cacheFile = new File(C.NET_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = chain -> {
Request request = chain.request();
if (!Util.isNetworkConnected(BaseApplication.getAppContext())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
Response.Builder newBuilder = response.newBuilder();
if (Util.isNetworkConnected(BaseApplication.getAppContext())) {
int maxAge = 0;
// 有网络时 设置缓存超时时间0个小时
newBuilder.header("Cache-Control", "public, max-age=" + maxAge);
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
newBuilder.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale);
}
return newBuilder.build();
};
builder.cache(cache).addInterceptor(cacheInterceptor);
if (BuildConfig.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
// 设置超时
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
// 错误重连
builder.retryOnConnectionFailure(true);
sOkHttpClient = builder.build();
}
Aggregations