Example usage for com.squareup.okhttp Response cacheResponse

List of usage examples for com.squareup.okhttp Response cacheResponse

Introduction

In this page you can find the example usage for com.squareup.okhttp Response cacheResponse.

Prototype

Response cacheResponse

To view the source code for com.squareup.okhttp Response cacheResponse.

Click Source Link

Usage

From source file:com.anony.okhttp.sample.CacheResponse.java

License:Apache License

public void run() throws Exception {
    Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();

    Response response1 = client.newCall(request).execute();
    if (!response1.isSuccessful())
        throw new IOException("Unexpected code " + response1);

    String response1Body = response1.body().string();
    System.out.println("Response 1 response:          " + response1);
    System.out.println("Response 1 cache response:    " + response1.cacheResponse());
    System.out.println("Response 1 network response:  " + response1.networkResponse());

    Response response2 = client.newCall(request).execute();
    if (!response2.isSuccessful())
        throw new IOException("Unexpected code " + response2);

    String response2Body = response2.body().string();
    System.out.println("Response 2 response:          " + response2);
    System.out.println("Response 2 cache response:    " + response2.cacheResponse());
    System.out.println("Response 2 network response:  " + response2.networkResponse());

    System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
}

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;//  w  w  w .j  a  va  2 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: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.  ja  va2 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:de.dev.eth0.rssreader.core.http.callback.AbstractFeedLoaderCallback.java

License:Apache License

@Override
public void onResponse(Response response) throws IOException {
    Timber.d("onSuccess %d (cache: %s network: %s)", response.code(), response.cacheResponse(),
            response.networkResponse());
    if (response.isSuccessful() && response.networkResponse() != null
            && response.networkResponse().code() != HttpURLConnection.HTTP_NOT_MODIFIED) {
        List<FeedEntry> summaries = RssFeedParser.parseFeed(response.body().byteStream());
        DataProviderHelper helper = getDataProviderHelper();
        List<FeedEntry> added = helper.insertFeedEntry(summaries);
        getNotificationManger().sendFeedUpdatedNotification(added);

        getPrecacheHelper().precache(summaries);
    }//from   www.  jav  a  2 s . c  om
    afterCallback();
}

From source file:de.dev.eth0.rssreader.core.http.callback.PrecacheCallback.java

License:Apache License

@Override
public void onResponse(Response response) throws IOException {
    Timber.d("onSuccess %d (cache: %s network: %s)", response.code(), response.cacheResponse(),
            response.networkResponse());
}

From source file:io.apiman.gateway.platforms.servlet.connectors.ok.HttpURLConnectionImpl.java

License:Apache License

private static String responseSourceHeader(Response response) {
    if (response.networkResponse() == null) {
        if (response.cacheResponse() == null) {
            return "NONE";
        }/*w ww .  ja  v  a  2 s  .  c om*/
        return "CACHE " + response.code();
    }
    if (response.cacheResponse() == null) {
        return "NETWORK " + response.code();
    }
    return "CONDITIONAL_CACHE " + response.networkResponse().code();
}