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


94

我将发送一个简单的get方法到我的服务器(这是Rails应用),并使用RxJava和Retrofit获得结果。我所做的是:

我的界面:

public interface ApiCall {
    String SERVICE_ENDPOINT = "https://198.50.214.15";
    @GET("/api/post")
    io.reactivex.Observable<Post> getPost();
}

我的模型是这样的:

public class Post
{
    @SerializedName("id")
    private String id;
    @SerializedName("body")
    private String body;
    @SerializedName("title")
    private String title;

    public String getId ()
    {
        return id;
    }


    public String getBody ()
    {
        return body;
    }


    public String getTitle ()
    {
        return title;
    }

}

这就是我在活动中所做的:

public class Javax extends AppCompatActivity {
    RecyclerView rvListContainer;
    postAdapter postAdapter;
    List<String> messageList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_javax);

        rvListContainer=(RecyclerView)findViewById(R.id.recyclerView);
        postAdapter=new postAdapter(messageList);

        rvListContainer.setAdapter(postAdapter);
    }
    @Override
    protected void onResume() {
        super.onResume();
        Retrofit retrofit=new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://198.50.214.15")
                .build();
        ApiCall apiService=retrofit.create(ApiCall.class);

        Observable<Post> observable=apiService.getPost();

        observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(responseData -> {
                    messageList.add(responseData.getTitle());
                    postAdapter.notifyDataSetChanged();
                });

    }
}

我不知道为什么会收到此错误,该错误说明我的适配器有问题。我还将适配器纳入了gradle中:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hussein.sqlitedatabase, PID: 19445
java.lang.RuntimeException: Unable to resume activity {com.example.hussein.sqlitedatabase/com.example.hussein.sqlitedatabase.Javax}: java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable<com.example.hussein.sqlitedatabase.Post>
    for method ApiCall.getPost
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2964)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2993)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5292)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable<com.example.hussein.sqlitedatabase.Post>
    for method ApiCall.getPost
    at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:751)
    at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:236)
    at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:161)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:169)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:146)
    at $Proxy0.getPost(Native Method)
    at com.example.hussein.sqlitedatabase.Javax.onResume(Javax.java:42)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1197)
    at android.app.Activity.performResume(Activity.java:5343)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
        ... 12 more
 Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.Observable<com.example.hussein.sqlitedatabase.Post>.
  Tried:
   * retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
   * retrofit2.ExecutorCallAdapterFactory
    at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:240)
    at retrofit2.Retrofit.callAdapter(Retrofit.java:204)
    at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:234)
        ... 20 more

这是我的Gradle依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'

    compile 'com.squareup.retrofit2:retrofit:2.2.0'

    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'

    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

    compile 'com.squareup.retrofit2:converter-gson:2.0.0'

    compile 'com.google.code.gson:gson:2.4'

}

Answers:


219

您需要使用以下命令告知Retrofit您要使用RxJava 2:

addCallAdapterFactory(RxJava2CallAdapterFactory.create())

因此,对于创建Retrofit对象,您将具有以下内容:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(SERVICE_ENDPOINT)
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build();

谢谢,但是我得到的另一个问题是我的网址,它说--->无法连接到/198.50.214.15:80
侯赛因·奥贾吉

我改为此.baseUrl(“ 198.50.214.15/”),但仍然遇到相同的错误
Hussein Ojaghi

4
这是内部带有RxJava2CallAdapterFactory的库的链接:github.com/square/retrofit/tree/master/retrofit-adapters/…–
Chuck

呜呜 非常感谢。
VipPunkJoshers Droopy

72

我遇到了同样的问题。除了添加新的存储库,您还可以添加Jake Wharton库的依赖项:

implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

然后您可以添加工厂:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.example.com")
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build();

我认为适配器具有版本2 ..的事实并不意味着它打算与RxJava 2一起使用。

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.1.0'

@ShaktisinhJadeja谢谢男人,感到很高兴我在您的帮助下
Raut Darpan

7
您需要使用com.squareup.retrofit2:adapter-rxjava2。请注意,您错过了2 :)
deral

RxJava2CallAdapterFactory不再使用-现在RxJavaCallAdapterFactory支持所有功能
Konrad Krakowiak

13

问题在于正在使用的库。

我已更换

implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0'

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

并使用

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL).client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

你们两个年级的成绩都一样?
Nadeem Shukoor

我已将rxjava:2.4.0替换为rxjava2:2.4.0
Anudeep '19
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.