Example usage for com.squareup.okhttp CacheControl.Builder noCache

List of usage examples for com.squareup.okhttp CacheControl.Builder noCache

Introduction

In this page you can find the example usage for com.squareup.okhttp CacheControl.Builder noCache.

Prototype

boolean noCache

To view the source code for com.squareup.okhttp CacheControl.Builder noCache.

Click Source Link

Usage

From source file:com.example.ivy.picassodemo.MyOkHttpDownloader.java

License:Apache License

@Override
public Response load(Uri uri, int networkPolicy) throws IOException {
    CacheControl cacheControl = null;/*from www  .j a  v  a2s  .c  o m*/
    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();
        }
    }

    Request.Builder builder = new Request.Builder().url(uri.toString());
    if (cacheControl != null) {
        builder.cacheControl(cacheControl);
    }

    com.squareup.okhttp.Response response = client.newCall(builder.build()).execute();
    int responseCode = response.code();
    if (responseCode >= 300) {
        response.body().close();
        throw new ResponseException(responseCode + " " + response.message(), networkPolicy, responseCode);
    }

    boolean fromCache = response.cacheResponse() != null;

    ResponseBody responseBody = response.body();
    return new Response(responseBody.byteStream(), fromCache, responseBody.contentLength());
}

From source file:com.squareup.picasso252.OkHttpDownloader.java

License:Apache License

@Override
public Response load(@NonNull Uri uri, int networkPolicy) throws IOException {
    CacheControl cacheControl = null;//from   w w  w  . j  av a2 s .  c  om
    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();
        }
    }

    Request.Builder builder = new Request.Builder().url(uri.toString());
    if (cacheControl != null) {
        builder.cacheControl(cacheControl);
    }

    com.squareup.okhttp.Response response = client.newCall(builder.build()).execute();
    int responseCode = response.code();
    if (responseCode >= 300) {
        response.body().close();
        throw new ResponseException(responseCode + " " + response.message(), networkPolicy, responseCode);
    }

    boolean fromCache = response.cacheResponse() != null;

    ResponseBody responseBody = response.body();
    return new Response(responseBody.byteStream(), fromCache, responseBody.contentLength());
}

From source file:es.upv.grycap.coreutils.fiber.http.Http2Client.java

License:Apache License

/**
 * Retrieve information from a server via a HTTP GET request.
 * @param url - URL target of this request
 * @param acceptableMediaTypes - Content-Types that are acceptable for this request
 * @param nocache - don't accept an invalidated cached response, and don't store the server's response in any cache
 * @param callback - is called back when the response is readable
 *//*from  w  w  w .j ava 2s.co  m*/
public void asyncGet(final String url, final @Nullable List<String> acceptableMediaTypes, final boolean nocache,
        final Callback callback) {
    final String url2 = requireNonNull(trimToNull(url), "A non-empty URL expected");
    requireNonNull(callback, "A valid callback expected");
    // configure cache
    final CacheControl.Builder cacheControlBuilder = new CacheControl.Builder();
    if (nocache)
        cacheControlBuilder.noCache().noStore();
    else
        cacheControlBuilder.maxStale(3600, TimeUnit.SECONDS);
    // prepare request
    final Request.Builder requestBuilder = new Request.Builder().cacheControl(cacheControlBuilder.build())
            .url(url2);
    ofNullable(acceptableMediaTypes).orElse(emptyList()).stream().filter(Objects::nonNull)
            .forEach(type -> requestBuilder.addHeader("Accept", type));
    // submit request
    client.newCall(requestBuilder.build()).enqueue(callback);
}