Android-使用RxJava进行改装会导致IllegalArgumentException

Android-使用RxJava进行改装会导致IllegalArgumentException,java,android,retrofit2,rx-java2,Java,Android,Retrofit2,Rx Java2,我尝试在安卓系统中使用RxJava进行改造,以链接网络请求。但正如我在标题中所说的,它导致了一个非法的辩论例外。在下面,您可以看到我迄今为止编写的代码(我还包括导入和gradle文件,以及我在项目中使用的依赖项): ApiClient.java: import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonCo

我尝试在安卓系统中使用RxJava进行改造,以链接网络请求。但正如我在标题中所说的,它导致了一个非法的辩论例外。在下面,您可以看到我迄今为止编写的代码(我还包括导入和gradle文件,以及我在项目中使用的依赖项):

ApiClient.java:

import retrofit2.Retrofit;

import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Path;
import rx.Observable;


public class ApiClient {

    // trailing slash is needed
    public static final String BASE_URL = "....";

    private static CliqueDBApiInterface sCliqueDBApiInterface;

    public static CliqueDBApiInterface getCliqueDBApiInterface(){

        if(sCliqueDBApiInterface == null){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();

            sCliqueDBApiInterface = retrofit.create(CliqueDBApiInterface.class);
        }

        return sCliqueDBApiInterface;
    }

    public interface CliqueDBApiInterface{
        @GET("statementsOfClique/{idOfClique}")
        //Call<ListOfStatements> getStatementsOfTheClique(@Path("idOfClique") int id, @Header("Authorization") String authHeader );
        Observable<ListOfStatements> getStatementsOfTheClique(@Path("idOfClique") int id, @Header("Authorization") String authHeader );
    }

}
但应用程序崩溃,我在LogCat中得到以下错误/异常:

03-13 18:36:29.190 4128-4128/com.celik.abdullah.hefuxi11 E/AndroidRuntime:致命异常:主 进程:com.celik.abdullah.hefuxi11,PID:4128 java.lang.IllegalArgumentException:无法为rx.Observable创建调用适配器 对于CliqueDBApiInterface.getStatementsOfTheClique方法 在2.Utils.methodError处(Utils.java:52) 在2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:60)中 HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:34) 在2.ServiceMethod.parseAnnotations上(ServiceMethod.java:36) 在Refundation2.Refundation.loadServiceMethod(Refundation.java:168)中 在reformation2.reformation$1.invoke(reformation.java:147) 位于java.lang.reflect.Proxy.invoke(Proxy.java:913) 位于$Proxy1.getStatementsOfTheClique(未知来源) 位于com.celik.abdullah.hefuxi11.adapters.CliqueAdapter$1.run(CliqueAdapter.java:124) 位于android.os.Handler.handleCallback(Handler.java:790) 位于android.os.Handler.dispatchMessage(Handler.java:99) 位于android.os.Looper.loop(Looper.java:164) 位于android.app.ActivityThread.main(ActivityThread.java:6494) 位于java.lang.reflect.Method.invoke(本机方法) 位于com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 原因:java.lang.IllegalArgumentException:找不到的调用适配器 可观察的。 尝试: *改装2.adapter.rxjava2.RxJava2CallAdapterFactory *改造2.2完全未来的适配器工厂 *改装2.2.1适配器工厂 在reformation2.reformation.nextCalAdapter(reformation.java:239)中 在reformation2.reformation.callAdapter(reformation.java:203)中 在2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:58)中 ... 14多

有人知道如何解决这个问题吗?我发现了一些关于这个异常的其他问题,并通过在gradle文件中播放/更改导入包尝试了所有答案,但没有任何效果。在构建改造实例时,我还添加了addCallAdapterFactory(RxJava2CallAdapterFactory.create()),但我得到了相同的错误


提前感谢,

要创建一个又一个呼叫,请在
subscribe()
方法中使用而不是创建
订户。

要创建一个又一个呼叫,请在
subscribe()中使用而不是创建
订户
方法。

您使用的是RxJava1.x observable和订阅服务器。您已经在
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())添加了RxJava 2.x的适配器工厂。

您应该从rxjava2.x导入

因此,您也需要更新依赖关系

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'




import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;

您正在使用RXJava1.x observable和订阅服务器。您已经在
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())添加了RxJava 2.x的适配器工厂。

您应该从rxjava2.x导入

因此,您也需要更新依赖关系

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'




import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;

是的,要么将所有内容更新为RxJava2,要么使用RxJava1

在这种情况下,您的导入将类似于:

/* rx */
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.3.2'

/* retrofit, gson */
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
然后你的界面会是这样的:

public static CliqueDBApiInterface getCliqueDBApiInterface(){

    if(sCliqueDBApiInterface == null){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        sCliqueDBApiInterface = retrofit.create(CliqueDBApiInterface.class);
    }

    return sCliqueDBApiInterface;
}


where okHttpClient = new OkHttpClient.Builder().build();

是的,要么将所有内容更新为RxJava2,要么使用RxJava1

在这种情况下,您的导入将类似于:

/* rx */
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.3.2'

/* retrofit, gson */
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
然后你的界面会是这样的:

public static CliqueDBApiInterface getCliqueDBApiInterface(){

    if(sCliqueDBApiInterface == null){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        sCliqueDBApiInterface = retrofit.create(CliqueDBApiInterface.class);
    }

    return sCliqueDBApiInterface;
}


where okHttpClient = new OkHttpClient.Builder().build();