Example usage for com.squareup.okhttp Response networkResponse

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

Introduction

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

Prototype

Response networkResponse

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

Click Source Link

Usage

From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java

License:Open Source License

@Test
public void networkInterceptorModifiedRequestIsReturned()
        throws IOException, InterruptedException, ExecutionException {
    server.enqueue(new MockResponse());

    Interceptor modifyHeaderInterceptor = new Interceptor() {
        @Override/*from  ww w.j  av  a 2  s  .c o  m*/
        public Response intercept(Chain chain) throws IOException {
            return chain
                    .proceed(chain.request().newBuilder().header("User-Agent", "intercepted request").build());
        }
    };

    client.networkInterceptors().add(modifyHeaderInterceptor);

    Request request = new Request.Builder().url(server.getUrl("/")).header("User-Agent", "user request")
            .build();

    Response response = FiberOkHttpUtil.executeInFiber(client, request);
    assertNotNull(response.request().header("User-Agent"));
    assertEquals("user request", response.request().header("User-Agent"));
    assertEquals("intercepted request", response.networkResponse().request().header("User-Agent"));
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Test
public void networkInterceptorModifiedRequestIsReturned() throws IOException {
    server.enqueue(new MockResponse());

    Interceptor modifyHeaderInterceptor = new Interceptor() {
        @Override/*from   w ww  . j  a  va  2 s .  c  o  m*/
        public Response intercept(Chain chain) throws IOException {
            return chain
                    .proceed(chain.request().newBuilder().header("User-Agent", "intercepted request").build());
        }
    };

    client.networkInterceptors().add(modifyHeaderInterceptor);

    Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "user request").build();

    Response response = client.newCall(request).execute();
    assertNotNull(response.request().header("User-Agent"));
    assertEquals("user request", response.request().header("User-Agent"));
    assertEquals("intercepted request", response.networkResponse().request().header("User-Agent"));
}

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.anony.okhttp.sample.RewriteResponseCacheControl.java

License:Apache License

public void run() throws Exception {
    for (int i = 0; i < 5; i++) {
        System.out.println("    Request: " + i);

        Request request = new Request.Builder().url("https://api.github.com/search/repositories?q=http")
                .build();/*from  www  .  ja  v  a 2 s  .c om*/

        if (i == 2) {
            // Force this request's response to be written to the cache. This way, subsequent responses
            // can be read from the cache.
            System.out.println("Force cache: true");
            client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
        } else {
            System.out.println("Force cache: false");
            client.networkInterceptors().clear();
        }

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

        System.out.println("    Network: " + (response.networkResponse() != null));
        System.out.println();
    }
}

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  w  w  w. j  a v a2  s. c o m*/
    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  .  j a  v  a  2s .c  o  m*/
        return "CACHE " + response.code();
    }
    if (response.cacheResponse() == null) {
        return "NETWORK " + response.code();
    }
    return "CONDITIONAL_CACHE " + response.networkResponse().code();
}

From source file:quickbeer.android.next.network.utils.LoginRedirectInterceptor.java

License:Open Source License

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Response response = chain.proceed(request);

    if (request.uri().getPath().equals("/Signin_r.asp") && response.isRedirect()) {
        Log.d(TAG, "Modifying response for login request");

        return new Response.Builder().request(request).protocol(response.protocol()).code(200)
                .message(response.message()).handshake(response.handshake()).headers(response.headers())
                .body(response.body()).networkResponse(response.networkResponse()).build();
    }/*from w w w.j  a  v a2  s . c  o  m*/

    return response;
}