Retrofit2 + Rxjava2 报错 java.lang.IllegalArgumentException: Could not locate call adapter for

报错信息如下:
java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.Flowable…

原因:


  • 没有为Retrofit设置AdapterFactory
  • 设置的AdapterFactory版本不对

解决方法:


  • 添加依赖
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
  • 设置AdapterFactory
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
private Retrofit createRetrofit(Retrofit.Builder builder, OkHttpClient client, String url) {
        return builder
                .baseUrl(url)
                .client(client)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

一定要主要引入的Rxjava2的版本要与添加AdapterFactory版本一致。

猜你喜欢

转载自blog.csdn.net/qin_shi/article/details/80449405
今日推荐