Search in sources :

Example 1 with GsonConverterFactory

use of retrofit2.converter.gson.GsonConverterFactory 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)

Example 2 with GsonConverterFactory

use of retrofit2.converter.gson.GsonConverterFactory in project RxJavaSamples by rengwuxian.

the class Network method getZhuangbiApi.

public static ZhuangbiApi getZhuangbiApi() {
    if (zhuangbiApi == null) {
        Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).baseUrl("http://www.zhuangbi.info/").addConverterFactory(gsonConverterFactory).addCallAdapterFactory(rxJavaCallAdapterFactory).build();
        zhuangbiApi = retrofit.create(ZhuangbiApi.class);
    }
    return zhuangbiApi;
}
Also used : Retrofit(retrofit2.Retrofit) ZhuangbiApi(com.rengwuxian.rxjavasamples.network.api.ZhuangbiApi)

Example 3 with GsonConverterFactory

use of retrofit2.converter.gson.GsonConverterFactory in project RxJavaSamples by rengwuxian.

the class Network method getGankApi.

public static GankApi getGankApi() {
    if (gankApi == null) {
        Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).baseUrl("http://gank.io/api/").addConverterFactory(gsonConverterFactory).addCallAdapterFactory(rxJavaCallAdapterFactory).build();
        gankApi = retrofit.create(GankApi.class);
    }
    return gankApi;
}
Also used : Retrofit(retrofit2.Retrofit) GankApi(com.rengwuxian.rxjavasamples.network.api.GankApi)

Example 4 with GsonConverterFactory

use of retrofit2.converter.gson.GsonConverterFactory in project ListenerMusicPlayer by hefuyicoder.

the class NetworkModule method provideLastFMRetrofit.

@Provides
@Named("lastfm")
@PerApplication
Retrofit provideLastFMRetrofit() {
    String endpointUrl = Constants.BASE_API_URL_LASTFM;
    Gson gson = new GsonBuilder().create();
    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().cache(new Cache(FileUtil.getHttpCacheDir(ListenerApp.getContext()), Constants.HTTP_CACHE_SIZE)).connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(Constants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS).build();
    //        OkHttpClient newClient = client.newBuilder().addInterceptor(loggingInterceptor).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(endpointUrl).client(client).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit;
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Named(javax.inject.Named) PerApplication(io.hefuyi.listener.injector.scope.PerApplication) Provides(dagger.Provides)

Example 5 with GsonConverterFactory

use of retrofit2.converter.gson.GsonConverterFactory in project ListenerMusicPlayer by hefuyicoder.

the class NetworkModule method provideKuGouRetrofit.

@Provides
@Named("kugou")
@PerApplication
Retrofit provideKuGouRetrofit() {
    String endpointUrl = Constants.BASE_API_URL_KUGOU;
    Gson gson = new GsonBuilder().create();
    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().cache(new Cache(FileUtil.getHttpCacheDir(ListenerApp.getContext()), Constants.HTTP_CACHE_SIZE)).connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(Constants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS).build();
    //        OkHttpClient newClient = client.newBuilder().addInterceptor(loggingInterceptor).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(endpointUrl).client(client).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit;
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Named(javax.inject.Named) PerApplication(io.hefuyi.listener.injector.scope.PerApplication) Provides(dagger.Provides)

Aggregations

Retrofit (retrofit2.Retrofit)5 GsonBuilder (com.google.gson.GsonBuilder)3 GsonConverterFactory (retrofit2.converter.gson.GsonConverterFactory)3 Gson (com.google.gson.Gson)2 Provides (dagger.Provides)2 PerApplication (io.hefuyi.listener.injector.scope.PerApplication)2 Named (javax.inject.Named)2 Cache (okhttp3.Cache)2 OkHttpClient (okhttp3.OkHttpClient)2 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)2 GankApi (com.rengwuxian.rxjavasamples.network.api.GankApi)1 ZhuangbiApi (com.rengwuxian.rxjavasamples.network.api.ZhuangbiApi)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 MoshiConverterFactory (retrofit2.converter.moshi.MoshiConverterFactory)1 SimpleXmlConverterFactory (retrofit2.converter.simplexml.SimpleXmlConverterFactory)1