Retrofit2 和 rxjava3:java.lang.IllegalArgumentException:无法找到 io.reactivex.rxjava3.core.Observable 的调用适配器

abo*_*ndi 4 android adapter rx-java retrofit2 rx-java3

我想使用 Retrofit2 和 rxjava3 但我看到以下错误

Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.rxjava3.core.Observable<java.lang.Object>.
      Tried:
       * retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
       * retrofit2.CompletableFutureCallAdapterFactory
       * retrofit2.DefaultCallAdapterFactory
Run Code Online (Sandbox Code Playgroud)

此错误表明Rxjava 没有适配器,但我将其添加到下面的行中

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

public class ServiceGenerator {
  private static final String BASE_URL = "http://example.com/";
  private static final OkHttpClient okHttpClient = new OkHttpClient.Builder()

    .connectTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(30, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();

  public static <S> S createServiceSample(Class<S> serviceClass) {
    Retrofit.Builder builder =
      new Retrofit.Builder()
        .client(okHttpClient)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create());
    builder.baseUrl(BASE_URL);
    Retrofit retrofit = builder.build();
    return retrofit.create(serviceClass);
  }

}
Run Code Online (Sandbox Code Playgroud)

和我的 Api 接口

public interface ApiInterface {
  @Headers("Content-Type: application/json")
  @GET("api/v1/movies?q=[]&page=[1]")
  Observable<Object> check();
}
Run Code Online (Sandbox Code Playgroud)

并在活动中使用

  ApiInterface apiInterface = ServiceGenerator.createServiceSample(ApiInterface.class);
    Observable<Object> cryptoObservable = apiInterface.check();
    cryptoObservable.subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new Observer<Object>() {
        @Override
        public void onSubscribe(@NonNull Disposable d) {
          Log.i(TAG, "onSubscribe: ");
        }

        @Override
        public void onNext(@NonNull Object o) {
          Log.i(TAG, "onNext: " + new Gson().toJson(o));

        }

        @Override
        public void onError(@NonNull Throwable e) {
          Log.i(TAG, "onError: ");
        }

        @Override
        public void onComplete() {
          Log.i(TAG, "onComplete: ");
        }
      });
Run Code Online (Sandbox Code Playgroud)

和构建.gradle

    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.4'

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    implementation 'com.google.code.gson:gson:2.8.2'
Run Code Online (Sandbox Code Playgroud)

abo*_*ndi 9

您只需替换build.gradle中的以下行

implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
Run Code Online (Sandbox Code Playgroud)

用这条线

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
Run Code Online (Sandbox Code Playgroud)

替换ServiceGenerator中的以下行

.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
Run Code Online (Sandbox Code Playgroud)

用这条线

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
Run Code Online (Sandbox Code Playgroud)

这对我有用


归档时间:

查看次数:

3321 次

最近记录:

5 年,7 月 前