Search in sources :

Example 31 with Response

use of retrofit2.Response in project okhttp by square.

the class AsynchronousGet method run.

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

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try (ResponseBody responseBody = response.body()) {
                if (!response.isSuccessful())
                    throw new IOException("Unexpected code " + response);
                Headers responseHeaders = response.headers();
                for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }
                System.out.println(responseBody.string());
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 32 with Response

use of retrofit2.Response in project okhttp by square.

the class CacheResponse method run.

public void run() throws Exception {
    Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
    String response1Body;
    try (Response response1 = client.newCall(request).execute()) {
        if (!response1.isSuccessful())
            throw new IOException("Unexpected code " + response1);
        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());
    }
    String response2Body;
    try (Response response2 = client.newCall(request).execute()) {
        if (!response2.isSuccessful())
            throw new IOException("Unexpected code " + response2);
        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));
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException)

Example 33 with Response

use of retrofit2.Response in project retrofit by square.

the class ProtoConverterFactoryTest method serializeAndDeserialize.

@Test
public void serializeAndDeserialize() throws IOException, InterruptedException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<Phone> call = service.post(Phone.newBuilder().setNumber("(519) 867-5309").build());
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.getNumber()).isEqualTo("(519) 867-5309");
    RecordedRequest request = server.takeRequest();
    assertThat(request.getBody().readByteString()).isEqualTo(encoded);
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf");
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 34 with Response

use of retrofit2.Response in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeEmpty.

@Test
public void deserializeEmpty() throws IOException {
    server.enqueue(new MockResponse());
    Call<Phone> call = service.get();
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.hasNumber()).isFalse();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 35 with Response

use of retrofit2.Response in project retrofit by square.

the class CallTest method transportProblemAsync.

@Test
public void transportProblemAsync() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.getString().enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    Throwable failure = failureRef.get();
    assertThat(failure).isInstanceOf(IOException.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Aggregations

Response (okhttp3.Response)474 Request (okhttp3.Request)352 Test (org.junit.Test)237 IOException (java.io.IOException)188 Response (retrofit2.Response)156 ResponseBody (okhttp3.ResponseBody)148 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Call (okhttp3.Call)102 MockResponse (okhttp3.mockwebserver.MockResponse)99 Observable (rx.Observable)98 OkHttpClient (okhttp3.OkHttpClient)72 RequestBody (okhttp3.RequestBody)71 Callback (okhttp3.Callback)43 List (java.util.List)40 TestClients.clientRequest (keywhiz.TestClients.clientRequest)37 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 Interceptor (okhttp3.Interceptor)29 HttpUrl (okhttp3.HttpUrl)27 MediaType (okhttp3.MediaType)26 Buffer (okio.Buffer)25