Java Code Examples for retrofit2.converter.moshi.MoshiConverterFactory#create()

The following examples show how to use retrofit2.converter.moshi.MoshiConverterFactory#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpModule.java    From auth with MIT License 6 votes vote down vote up
public RedditAuthApi getAuthApiService() {
    if (authService == null) {
        synchronized (this) {
            if (authService == null) {
                MoshiConverterFactory converterFactory =
                        MoshiConverterFactory.create(provideMoshi());
                authService =
                        new Retrofit.Builder()
                                .client(provideOkHttp())
                                .addConverterFactory(converterFactory)
                                .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
                                .baseUrl("https://www.reddit.com/api/")
                                .build()
                                .create(RedditAuthApi.class);
            }
        }
    }
    return authService;
}
 
Example 2
Source File: HttpModule.java    From auth with MIT License 5 votes vote down vote up
public RedditApi getApiService() {
    if (apiService == null) {
        synchronized (this) {
            if (apiService == null) {
                OAuthAccountManager authenticator = app.getAccountManager();
                final OkHttpClient okHttpClient =
                        provideOkHttp()
                                .newBuilder()
                                // add authenticators only here to prevent deadlocks when
                                // (re-)authenticating
                                .authenticator(new RequestRetryAuthenticator(authenticator))
                                .addInterceptor(new RequestAuthInterceptor(authenticator))
                                .build();
                MoshiConverterFactory converterFactory =
                        MoshiConverterFactory.create(provideMoshi());
                apiService =
                        new Retrofit.Builder()
                                .client(okHttpClient)
                                .addConverterFactory(converterFactory)
                                .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
                                .baseUrl("https://oauth.reddit.com/api/")
                                .build()
                                .create(RedditApi.class);
            }
        }
    }
    return apiService;
}
 
Example 3
Source File: IclusionApiMain.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
private static IclusionApi buildIclusionApi(@NotNull OkHttpClient httpClient, @NotNull String iClusionEndpoint) {
    MoshiConverterFactory moshiConverterFactory =
            MoshiConverterFactory.create(new Moshi.Builder().add(new IclusionResponseAdapter()).build());

    Retrofit retrofit = new Retrofit.Builder().baseUrl(iClusionEndpoint)
            .addConverterFactory(moshiConverterFactory)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
            .client(httpClient)
            .build();

    return retrofit.create(IclusionApi.class);
}
 
Example 4
Source File: DataModule.java    From android with Apache License 2.0 4 votes vote down vote up
@Provides @Singleton Converter.Factory provideConverterFactory(Moshi moshi) {
  return MoshiConverterFactory.create(moshi);
}
 
Example 5
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 4 votes vote down vote up
/**
 * Constructs a new {@code InfluxDBImpl}.
 *
 * @param url
 *          The InfluxDB server API URL
 * @param username
 *          The InfluxDB user name
 * @param password
 *          The InfluxDB user password
 * @param okHttpBuilder
 *          The OkHttp Client Builder
 * @param retrofitBuilder
 *          The Retrofit Builder
 * @param responseFormat
 *          The {@code ResponseFormat} to use for response from InfluxDB
 *          server
 */
public InfluxDBImpl(final String url, final String username, final String password,
                    final OkHttpClient.Builder okHttpBuilder, final Retrofit.Builder retrofitBuilder,
                    final ResponseFormat responseFormat) {
  this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
  this.hostName = parseHost(url);

  this.loggingInterceptor = new HttpLoggingInterceptor();
  setLogLevel(LOG_LEVEL);

  this.gzipRequestInterceptor = new GzipRequestInterceptor();
  OkHttpClient.Builder clonedOkHttpBuilder = okHttpBuilder.build().newBuilder()
          .addInterceptor(loggingInterceptor)
          .addInterceptor(gzipRequestInterceptor);
  if (username != null && password != null) {
    clonedOkHttpBuilder.addInterceptor(new BasicAuthInterceptor(username, password));
  }
  Factory converterFactory = null;
  switch (responseFormat) {
  case MSGPACK:
    clonedOkHttpBuilder.addInterceptor(chain -> {
      Request request = chain.request().newBuilder().addHeader("Accept", APPLICATION_MSGPACK).build();
      return chain.proceed(request);
    });

    converterFactory = MessagePackConverterFactory.create();
    chunkProccesor = new MessagePackChunkProccesor();
    break;
  case JSON:
  default:
    converterFactory = MoshiConverterFactory.create();

    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<QueryResult> adapter = moshi.adapter(QueryResult.class);
    chunkProccesor = new JSONChunkProccesor(adapter);
    break;
  }

  this.client = clonedOkHttpBuilder.build();
  Retrofit.Builder clonedRetrofitBuilder = retrofitBuilder.baseUrl(url).build().newBuilder();
  this.retrofit = clonedRetrofitBuilder.client(this.client)
          .addConverterFactory(converterFactory).build();
  this.influxDBService = this.retrofit.create(InfluxDBService.class);

}