无法为io.reactivex.Single创建呼叫适配器

时间:2019-05-28 10:30:08

标签: android observable retrofit2 rx-java2

我想通过改造和rxjava来连接服务器。当我使用call时一切正常,但是一切正常。但是当尝试使用rxjava时,它陷入麻烦。 错误文字:

无法找到io.reactivex.Single的呼叫适配器

在build.gradle中,我实现了改造适配器。但是我不知道出了什么问题。 这是我的礼物:

implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

api客户端代码:

public class ApiClient {
public static final String BASE_URL="http://192.168.1.100/digikala/";
private static Retrofit retrofit=null;
public static Retrofit getClient(){
    if(retrofit==null){
        retrofit=new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
    }

    return  retrofit;
}

api服务代码:

public interface ApiService {

   @GET("readamazing.php")
   Single<List<Product>> getSingleProducts();
 }

主要激励代码:

ApiService apiService=ApiClient.getClient().create(ApiService.class);
apiService.getSingleProducts().subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new SingleObserver<List<Product>>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onSuccess(List<Product> products) {
            Log.i("LOG", "onSuccess: "+products.toString());
        }

        @Override
        public void onError(Throwable e) {
            Log.i("LOG", "onSuccess: "+e.toString());
        }
    });

1 个答案:

答案 0 :(得分:0)

使用RxJava2CallAdapterFactory代替使用RxJavaCallAdapterFactory RxJava2

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

Retrofit.Builder()