com.squareup.okhttp.CacheControl Java Examples

The following examples show how to use com.squareup.okhttp.CacheControl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Downloader.java    From external-resources with Apache License 2.0 5 votes vote down vote up
public Resources load(@Cache.Policy int policy) throws ExternalResourceException {
  buildUrl();

  Logger.i(ExternalResources.TAG, "Load configuration from url: " + url.build());

  final CacheControl cacheControl;
  switch (policy) {
    case Cache.POLICY_NONE:
      cacheControl = new CacheControl.Builder().noCache().noStore().build();
      break;
    case Cache.POLICY_OFFLINE:
      cacheControl = CacheControl.FORCE_CACHE;
      break;
    case Cache.POLICY_ALL:
    default:
      cacheControl = new CacheControl.Builder().build();
      break;
  }

  Logger.v(ExternalResources.TAG, "CachePolicy: " + policy);

  Request request = new Request.Builder().url(url.build()).cacheControl(cacheControl).build();

  try {
    Response response = client.newCall(request).execute();
    int responseCode = response.code();

    Logger.d(ExternalResources.TAG, "Response code: " + responseCode);
    if (responseCode >= 300) {
      response.body().close();
      throw new ResponseException(responseCode + " " + response.message(), policy, responseCode);
    }

    return converter.fromReader(response.body().charStream());
  } catch (IOException e) {
    throw new ExternalResourceException(e);
  }
}
 
Example #2
Source File: HttpClient.java    From IndiaSatelliteWeather with GNU General Public License v2.0 5 votes vote down vote up
public static Request generateRequest(String url, CacheType cacheType) {
    Request.Builder builder = new Request.Builder().url(url);
    if (cacheType == CacheType.USE_CACHE) {
        //Using cache, so the same call wont happen again for next 1 day.
        builder.cacheControl(new CacheControl.Builder()
                .maxStale(1, TimeUnit.DAYS)
                .build());
    }
    return builder.build();
}
 
Example #3
Source File: Ok2Lite.java    From httplite with Apache License 2.0 4 votes vote down vote up
private com.squareup.okhttp.Request makeRequest(Request request){
    com.squareup.okhttp.Request.Builder rb = new com.squareup.okhttp.Request.Builder().url(request.getUrl()).tag(request.getTag());
    Headers headers = createHeader(request.getHeaders());
    if(headers!=null) rb.headers(headers);
    alexclin.httplite.RequestBody liteBody = request.getRequestBody();
    RequestBody requestBody = null;
    if(liteBody!=null){
        requestBody = mFactory.convertBody(liteBody,request.getWrapListener());
    }
    switch (request.getMethod()){
        case GET:
            rb = rb.get();
            break;
        case POST:
            rb = rb.post(requestBody);
            break;
        case PUT:
            rb = rb.put(requestBody);
            break;
        case PATCH:
            rb = rb.patch(requestBody);
            break;
        case HEAD:
            rb = rb.head();
            break;
        case DELETE:
            if(requestBody==null){
                rb = rb.delete();
            }else{
                rb = rb.delete(requestBody);
            }
            break;
    }
    if(request.getCacheExpiredTime()>0){
        rb.cacheControl(new CacheControl.Builder().maxAge(request.getCacheExpiredTime(), TimeUnit.SECONDS).build());
    }else if(request.getCacheExpiredTime()== alexclin.httplite.Request.FORCE_CACHE){
        rb.cacheControl(CacheControl.FORCE_CACHE);
    }else if(request.getCacheExpiredTime()== alexclin.httplite.Request.NO_CACHE){
        rb.cacheControl(CacheControl.FORCE_NETWORK);
    }
    return rb.build();
}
 
Example #4
Source File: HttpApiBase.java    From iview-android-tv with MIT License 4 votes vote down vote up
protected CacheControl allowStaleCache(int seconds) {
    return new CacheControl.Builder().maxStale(seconds, TimeUnit.SECONDS).build();
}