com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory Java Examples

The following examples show how to use com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory. 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: AppApiHelper.java    From InstantAppStarter with MIT License 6 votes vote down vote up
private ApiClient getApiClient() {

        OkHttpClient.Builder client = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client.addInterceptor(loggingInterceptor);

        OkHttpClient myClient = client.build();

        if (BuildConfig.DEBUG) {
            IdlingResources.registerOkHttp(myClient);
        }

        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .client(myClient)
                .addConverterFactory(RaveConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(ApiClient.class);
    }
 
Example #2
Source File: ImgurApi.java    From RxPalette with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new ImgurApi with the specified clientId.
 *
 * @param clientId The client id.
 */
public ImgurApi(final String clientId) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request request = chain.request();
                    request = request.newBuilder().addHeader("Authorization", "Client-ID " + clientId).build();
                    return chain.proceed(request);
                }
            })
            .build();
    albumService = new Retrofit.Builder()
            .baseUrl("https://api.imgur.com/3/")
            .client(okHttpClient)
            .addConverterFactory(MoshiConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(AlbumService.class);
}
 
Example #3
Source File: ExampleUnitTest.java    From POCenter with MIT License 6 votes vote down vote up
@Test
    public void testSearch() throws IOException {
        RemoteApi remoteApi = new Retrofit.Builder()
                .baseUrl(URL_BASE)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create())
                .client(UnsafeOkHttpUtils.getClient())
                .build()
                .create(RemoteApi.class);
        remoteApi.getList(0, 20, null, 0, new String[]{"微信"})
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(ResponseBody responseBody) throws Exception {
                        String string = responseBody.string();
                        System.out.println(string);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });

    }
 
Example #4
Source File: RetrofitModule.java    From flowless with Apache License 2.0 5 votes vote down vote up
@Provides
@ActivityScope
Retrofit retrofit(OkHttpClient okHttpClient, RxJava2CallAdapterFactory rxJava2CallAdapterFactory) {
    return new Retrofit.Builder().addConverterFactory(LoganSquareConverterFactory.create()) //
            .baseUrl("https://api.github.com/") //
            .addCallAdapterFactory(rxJava2CallAdapterFactory)
            .client(okHttpClient) //
            .build();
}
 
Example #5
Source File: Repertories.java    From POCenter with MIT License 5 votes vote down vote up
public Repertories(File cacheDir) {
    // create apis
    cacheApi = new RxCache.Builder()
            .useExpiredDataIfLoaderNotAvailable(true)
            .persistence(cacheDir, new GsonSpeaker())
            .using(CacheApi.class);

    remoteApi = new Retrofit.Builder()
            .baseUrl(URL_BASE)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(UnsafeOkHttpUtils.getClient())
            .build()
            .create(RemoteApi.class);

    // add types
    Context context = BaseApplication.getContext();
    types.add(context.getString(R.string.type_other));
    types.add(context.getString(R.string.type_web));
    types.add(context.getString(R.string.type_we_chat));
    types.add(context.getString(R.string.type_html5));
    types.add(context.getString(R.string.type_app));
    types.add(context.getString(R.string.type_intelligent_hardware));
    types.add(context.getString(R.string.type_desktop_app));
    types.add(context.getString(R.string.type_big_data));
    types.add(context.getString(R.string.type_system));
    types.add(context.getString(R.string.type_sdk_api));
    types.add(context.getString(R.string.type_art));
}
 
Example #6
Source File: MainActivity.java    From okhttp-json-mock with Apache License 2.0 5 votes vote down vote up
private Retrofit constructRetrofit(OkHttpClient okHttpClient) {
    return new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl(BASE_URL)
            .client(okHttpClient)
            .build();
}
 
Example #7
Source File: RetrofitHelper.java    From RxJava2RetrofitDemo with Apache License 2.0 5 votes vote down vote up
public static Retrofit getJokeRetrofit() {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.Url.JOKE)
            .addConverterFactory(new NullOnEmptyConverterFactory())
            .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(OkHttpHelper.getOkClient())
            .build();
    return retrofit;
}
 
Example #8
Source File: NetModule.java    From exchange-rates-mvvm with Apache License 2.0 5 votes vote down vote up
@Override
@Provides
@Singleton
public Retrofit provideRetrofit(Gson gson, ApplicationProperties propertiesManager, OkHttpClient client) {

    return new Retrofit.Builder()
            .baseUrl(propertiesManager.baseUrl())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client)
            .build();
}
 
Example #9
Source File: ProductHuntRestApi.java    From Capstone-Project with MIT License 5 votes vote down vote up
/**
 * Access the web services provided in {@link ProductHuntService}
 *
 * @return {@link ProductHuntService}
 */
public static ProductHuntService getApi() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Authorization.BASE_URL)
            .client(getOkHttpClient(true))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(ProductHuntService.class);
}
 
Example #10
Source File: ProductHuntRestApi.java    From Capstone-Project with MIT License 5 votes vote down vote up
public static ProductHuntService getSearchApi() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Authorization.SEARCH_URL)
            .client(getOkHttpClient(false))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(ProductHuntService.class);
}
 
Example #11
Source File: ProductHuntRestApi.java    From Capstone-Project with MIT License 5 votes vote down vote up
/**
 * Access the web services provided in {@link ProductHuntService}
 *
 * @return {@link ProductHuntService}
 */
public static ProductHuntService getApi() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Authorization.BASE_URL)
            .client(getOkHttpClient(true))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(ProductHuntService.class);
}
 
Example #12
Source File: ProductHuntRestApi.java    From Capstone-Project with MIT License 5 votes vote down vote up
public static ProductHuntService getSearchApi() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Authorization.SEARCH_URL)
            .client(getOkHttpClient(false))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(ProductHuntService.class);
}
 
Example #13
Source File: GithubService.java    From RxJava-Android-Samples with Apache License 2.0 5 votes vote down vote up
public static GithubApi createGithubService(final String githubToken) {
  Retrofit.Builder builder =
      new Retrofit.Builder()
          .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
          .addConverterFactory(GsonConverterFactory.create())
          .baseUrl("https://api.github.com");

  if (!TextUtils.isEmpty(githubToken)) {

    OkHttpClient client =
        new OkHttpClient.Builder()
            .addInterceptor(
                chain -> {
                  Request request = chain.request();
                  Request newReq =
                      request
                          .newBuilder()
                          .addHeader("Authorization", format("token %s", githubToken))
                          .build();
                  return chain.proceed(newReq);
                })
            .build();

    builder.client(client);
  }

  return builder.build().create(GithubApi.class);
}
 
Example #14
Source File: RxJavaBaseActivity.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
private void withRetrofit2AndGson() {
    final OkHttpClient mClient = new OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build();

    final Retrofit mRetrofit = new Retrofit.Builder()
            .client(mClient)
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

    GankApi mGankApi = mRetrofit.create(GankApi.class);
    Observable<GankAndroid> mAndroidObservable = mGankApi.getData("10/1");
    mCompositeDisposable.add(mAndroidObservable
            .subscribeOn(Schedulers.io())
            .map(gankAndroid -> gankAndroid.getResults().get(0))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(resultsEntity -> {
                sb.append(resultsEntity.getCreatedAt()).append("\n")
                        .append(resultsEntity.getType()).append("\n")
                        .append(resultsEntity.getDesc()).append("\n")
                        .append(resultsEntity.getUrl()).append("\n")
                        .append(resultsEntity.getWho());

                logContent.setText(sb.toString());
            }));

}
 
Example #15
Source File: NetUnitTest.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
private Retrofit initRetrofit(Interceptor... interceptors) {
    final OkHttpClient mClient = initOkHttpClient(interceptors);

    final Retrofit mRetrofit = new Retrofit.Builder()
            .client(mClient)
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

    return mRetrofit;
}
 
Example #16
Source File: RetrofitManager.java    From gank with GNU General Public License v3.0 5 votes vote down vote up
private RetrofitManager(@HostType.HostTypeChecker int hostType) {
    LogUtil.e("RetrofitManager" + hostType);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.getHost(hostType))
            .client(getOkHttpClient())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();

    mNewsService = retrofit.create(NewsService.class);
    mGankService = retrofit.create(GankService.class);
}
 
Example #17
Source File: HttpRetrofit.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
private HttpRetrofit() {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create();
    retrofit = new Retrofit.Builder()
            .client(getOkHttpClient())
            .addConverterFactory(GsonConverterFactory.create(gson)) //  添加数据解析ConverterFactory
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //添加RxJava
            .baseUrl(Constants.API_URL)
            .build();
    apiService = retrofit.create(HttpService.class);
}
 
Example #18
Source File: RestApiTest.java    From Mockery with Apache License 2.0 5 votes vote down vote up
@Override protected RestApi restApi() {
  return new Retrofit.Builder()
      .baseUrl(RestApi.URL_BASE)
      .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
      .addConverterFactory(GsonConverterFactory.create())
      .build().create(RestApi.class);
}
 
Example #19
Source File: MainActivity.java    From android-rxjava-retrofit with MIT License 5 votes vote down vote up
private void loadJSON() {


        RequestInterface requestInterface = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(RequestInterface.class);

        mCompositeDisposable.add(requestInterface.register()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this::handleResponse,this::handleError));
    }
 
Example #20
Source File: RetrofitProvider.java    From RxJava2Demo with Apache License 2.0 5 votes vote down vote up
public static Retrofit get() {
    OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
    builder.readTimeout(10, TimeUnit.SECONDS);
    builder.connectTimeout(9, TimeUnit.SECONDS);

    return new Retrofit.Builder().baseUrl(ENDPOINT)
            .client(builder.build())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
}
 
Example #21
Source File: RetrofitHelper.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 通过 域名 获取retrofit
 */
RetrofitHelper(String domain, OkHttpClient okHttpClient) {
    this.domain = domain;

    this.mRetrofit = new Retrofit.Builder()
            .baseUrl(domain)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
}
 
Example #22
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #23
Source File: HaprampApiClient.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
public static Retrofit getClient(final String token) {
  HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
  logging.setLevel(HttpLoggingInterceptor.Level.BODY);
  OkHttpClient client = new OkHttpClient
    .Builder()
    .connectTimeout(5, TimeUnit.MINUTES)
    .readTimeout(5, TimeUnit.MINUTES)
    .socketFactory(new RestrictedSocketFactory(size3))
    .addInterceptor(new Interceptor() {
      @Override
      public Response intercept(Chain chain) throws IOException {
        Request request = chain.request()
          .newBuilder()
          .addHeader("Authorization", "Token " + token)
          .build();
        return chain.proceed(request);
      }
    })
    .addInterceptor(logging)
    .build();

  retrofit = new Retrofit.Builder()
    .baseUrl(URLS.BASE_URL)
    .addConverterFactory(ScalarsConverterFactory.create())
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .client(client)
    .build();
  return retrofit;
}
 
Example #24
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #25
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #26
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #27
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #28
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #29
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}
 
Example #30
Source File: PortfolioModule.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
    return new Retrofit.Builder()
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://api.iextrading.com/1.0/")
            .build();
}