use of retrofit2.Call in project retrofit by square.
the class RetrofitTest method argumentCapture.
/** Confirm that Retrofit encodes parameters when the call is executed, and not earlier. */
@Test
public void argumentCapture() throws Exception {
AtomicInteger i = new AtomicInteger();
server.enqueue(new MockResponse().setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
MutableParameters mutableParameters = retrofit.create(MutableParameters.class);
i.set(100);
Call<String> call1 = mutableParameters.method(i);
i.set(101);
Response<String> response1 = call1.execute();
i.set(102);
assertEquals("a", response1.body());
assertEquals("/?i=101", server.takeRequest().getPath());
i.set(200);
Call<String> call2 = call1.clone();
i.set(201);
Response<String> response2 = call2.execute();
i.set(202);
assertEquals("b", response2.body());
assertEquals("/?i=201", server.takeRequest().getPath());
}
use of retrofit2.Call in project retrofit by square.
the class RetrofitTest method callAdapterFactoryNoMatchThrows.
@Test
public void callAdapterFactoryNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingCallAdapterFactory nonMatchingFactory = new NonMatchingCallAdapterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addCallAdapterFactory(nonMatchingFactory).build();
try {
retrofit.callAdapter(type, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate call adapter for class java.lang.String.\n" + " Tried:\n" + " * retrofit2.helpers.NonMatchingCallAdapterFactory\n" + " * retrofit2.DefaultCallAdapterFactory");
}
assertThat(nonMatchingFactory.called).isTrue();
}
use of retrofit2.Call in project retrofit by square.
the class DeserializeErrorBody method main.
public static void main(String... args) throws IOException {
// Create a local web server which response with a 404 and JSON body.
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse().setResponseCode(404).setBody("{\"message\":\"Unable to locate resource\"}"));
// Create our Service instance with a Retrofit pointing at the local web server and Gson.
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(GsonConverterFactory.create()).build();
Service service = retrofit.create(Service.class);
Response<User> response = service.getUser().execute();
// Normally you would check response.isSuccess() here before doing the following, but we know
// this call will always fail. You could also use response.code() to determine whether to
// convert the error body and/or which type to use for conversion.
// Look up a converter for the Error type on the Retrofit instance.
Converter<ResponseBody, Error> errorConverter = retrofit.responseBodyConverter(Error.class, new Annotation[0]);
// Convert the error body into our Error type.
Error error = errorConverter.convert(response.errorBody());
System.out.println("ERROR: " + error.message);
server.shutdown();
}
use of retrofit2.Call in project retrofit by square.
the class RetrofitTest method callAdapterFactoryDelegateNoMatchThrows.
@Test
public void callAdapterFactoryDelegateNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
DelegatingCallAdapterFactory delegatingFactory1 = new DelegatingCallAdapterFactory();
DelegatingCallAdapterFactory delegatingFactory2 = new DelegatingCallAdapterFactory();
NonMatchingCallAdapterFactory nonMatchingFactory = new NonMatchingCallAdapterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addCallAdapterFactory(delegatingFactory1).addCallAdapterFactory(delegatingFactory2).addCallAdapterFactory(nonMatchingFactory).build();
try {
retrofit.callAdapter(type, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate call adapter for class java.lang.String.\n" + " Skipped:\n" + " * retrofit2.helpers.DelegatingCallAdapterFactory\n" + " * retrofit2.helpers.DelegatingCallAdapterFactory\n" + " Tried:\n" + " * retrofit2.helpers.NonMatchingCallAdapterFactory\n" + " * retrofit2.DefaultCallAdapterFactory");
}
assertThat(delegatingFactory1.called).isTrue();
assertThat(delegatingFactory2.called).isTrue();
assertThat(nonMatchingFactory.called).isTrue();
}
use of retrofit2.Call in project retrofit by square.
the class RetrofitTest method callCallCustomAdapter.
@Test
public void callCallCustomAdapter() {
final AtomicBoolean factoryCalled = new AtomicBoolean();
final AtomicBoolean adapterCalled = new AtomicBoolean();
class MyCallAdapterFactory extends CallAdapter.Factory {
@Override
public CallAdapter<?, ?> get(final Type returnType, Annotation[] annotations, Retrofit retrofit) {
factoryCalled.set(true);
if (getRawType(returnType) != Call.class) {
return null;
}
return new CallAdapter<Object, Call<?>>() {
@Override
public Type responseType() {
return getParameterUpperBound(0, (ParameterizedType) returnType);
}
@Override
public Call<Object> adapt(Call<Object> call) {
adapterCalled.set(true);
return call;
}
};
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addCallAdapterFactory(new MyCallAdapterFactory()).build();
CallMethod example = retrofit.create(CallMethod.class);
assertThat(example.getResponseBody()).isNotNull();
assertThat(factoryCalled.get()).isTrue();
assertThat(adapterCalled.get()).isTrue();
}
Aggregations