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());
}
}
});
}
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));
}
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");
}
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();
}
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);
}
Aggregations