use of retrofit2.converter.moshi.MoshiConverterFactory 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.converter.moshi.MoshiConverterFactory in project retrofit by square.
the class MoshiConverterFactoryTest method setUp.
@Before
public void setUp() {
Moshi moshi = new Moshi.Builder().add(new JsonAdapter.Factory() {
@Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
for (Annotation annotation : annotations) {
if (!annotation.annotationType().isAnnotationPresent(JsonQualifier.class)) {
throw new AssertionError("Non-@JsonQualifier annotation: " + annotation);
}
}
return null;
}
}).add(new Adapters()).build();
MoshiConverterFactory factory = MoshiConverterFactory.create(moshi);
MoshiConverterFactory factoryLenient = factory.asLenient();
MoshiConverterFactory factoryNulls = factory.withNullSerialization();
MoshiConverterFactory factoryFailOnUnknown = factory.failOnUnknown();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factory).build();
Retrofit retrofitLenient = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factoryLenient).build();
Retrofit retrofitNulls = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factoryNulls).build();
Retrofit retrofitFailOnUnknown = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(factoryFailOnUnknown).build();
service = retrofit.create(Service.class);
serviceLenient = retrofitLenient.create(Service.class);
serviceNulls = retrofitNulls.create(Service.class);
serviceFailOnUnknown = retrofitFailOnUnknown.create(Service.class);
}
Aggregations