use of retrofit2.Retrofit in project retrofit by square.
the class AnnotatedConverters method main.
public static void main(String... args) throws IOException {
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse().setBody("{\"name\": \"Moshi\"}"));
server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));
server.enqueue(new MockResponse().setBody("<user name=\"SimpleXML\"/>"));
server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));
com.squareup.moshi.Moshi moshi = new com.squareup.moshi.Moshi.Builder().build();
com.google.gson.Gson gson = new GsonBuilder().create();
MoshiConverterFactory moshiConverterFactory = MoshiConverterFactory.create(moshi);
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
SimpleXmlConverterFactory simpleXmlConverterFactory = SimpleXmlConverterFactory.create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new AnnotatedConverterFactory.Builder().add(Moshi.class, moshiConverterFactory).add(Gson.class, gsonConverterFactory).add(SimpleXml.class, simpleXmlConverterFactory).build()).addConverterFactory(gsonConverterFactory).build();
Service service = retrofit.create(Service.class);
Library library1 = service.exampleMoshi().execute().body();
System.out.println("Library 1: " + library1.name);
Library library2 = service.exampleGson().execute().body();
System.out.println("Library 2: " + library2.name);
Library library3 = service.exampleSimpleXml().execute().body();
System.out.println("Library 3: " + library3.name);
Library library4 = service.exampleDefault().execute().body();
System.out.println("Library 4: " + library4.name);
server.shutdown();
}
use of retrofit2.Retrofit in project retrofit by square.
the class ChunkingConverter method main.
public static void main(String... args) throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
server.start();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ChunkingConverterFactory()).addConverterFactory(GsonConverterFactory.create()).build();
Service service = retrofit.create(Service.class);
Repo retrofitRepo = new Repo("square", "retrofit");
service.sendNormal(retrofitRepo).execute();
RecordedRequest normalRequest = server.takeRequest();
System.out.println("Normal @Body Transfer-Encoding: " + normalRequest.getHeader("Transfer-Encoding"));
service.sendChunked(retrofitRepo).execute();
RecordedRequest chunkedRequest = server.takeRequest();
System.out.println("@Chunked @Body Transfer-Encoding: " + chunkedRequest.getHeader("Transfer-Encoding"));
server.shutdown();
}
use of retrofit2.Retrofit 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.Retrofit in project retrofit by square.
the class RetrofitTest method requestConverterFactorySkippedNoMatchThrows.
@Test
public void requestConverterFactorySkippedNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingConverterFactory nonMatchingFactory1 = new NonMatchingConverterFactory();
NonMatchingConverterFactory nonMatchingFactory2 = new NonMatchingConverterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory1).addConverterFactory(nonMatchingFactory2).build();
try {
retrofit.nextRequestBodyConverter(nonMatchingFactory1, type, annotations, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate RequestBody converter for class java.lang.String.\n" + " Skipped:\n" + " * retrofit2.BuiltInConverters\n" + " * retrofit2.helpers.NonMatchingConverterFactory\n" + " Tried:\n" + " * retrofit2.helpers.NonMatchingConverterFactory");
}
assertThat(nonMatchingFactory1.called).isFalse();
assertThat(nonMatchingFactory2.called).isTrue();
}
use of retrofit2.Retrofit 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();
}
Aggregations