Search in sources :

Example 1 with SimpleXmlConverterFactory

use of retrofit2.converter.simplexml.SimpleXmlConverterFactory 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();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) SimpleXmlConverterFactory(retrofit2.converter.simplexml.SimpleXmlConverterFactory) Retrofit(retrofit2.Retrofit) MoshiConverterFactory(retrofit2.converter.moshi.MoshiConverterFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 Retrofit (retrofit2.Retrofit)1 GsonConverterFactory (retrofit2.converter.gson.GsonConverterFactory)1 MoshiConverterFactory (retrofit2.converter.moshi.MoshiConverterFactory)1 SimpleXmlConverterFactory (retrofit2.converter.simplexml.SimpleXmlConverterFactory)1