集成Retrofit2+RxJava2时,会出现这个错误
Unable to create call adapter for io.reactivex.Observable
然后配置
public Retrofit PROVIDRetrofit(OkHttpClient okHttpClient){ Retrofit.Builder builder = new Retrofit.Builder(); builder.baseUrl(ApiSever.BASE_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient); return builder.build(); }
如果你集成的Retrofit1+RxJava1,那么在配置的时候引用的是RxJavaCallAdapterFactory,但是如果是Retrofit2+RxJava2,需要用RxJava2CallAdapterFactory
builde文件(注意版本)
//retrofit implementation 'com.squareup.retrofit2:retrofit:2.1.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' implementation 'io.reactivex.rxjava2:rxjava:2.0.1' implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' // okHttp implementation 'com.squareup.okhttp3:okhttp:3.4.1' implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'