IllegalArgumentException: Could not locate call adapter for rx.Observable RxJava, Retrofit2

后端 未结 5 1325
野的像风
野的像风 2020-12-15 03:39

I am getting the above error while calling the rest api. I am using both retrofit2 and RxJava.

ServiceFactory.java

public class ServiceFactory {
pu         


        
相关标签:
5条回答
  • 2020-12-15 04:00

    In my case, it was enough to replace

    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    

    with

    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    
    0 讨论(0)
  • 2020-12-15 04:02

    you should have to use all Rx dependency of latest version , here i am using version 2 (like rxjava2)

    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    

    And add one more thing :

    addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    

    in Retrofit Api client like :

    retrofit = new Retrofit.Builder()
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .baseUrl(BASE_URL)
                        .build();
    
    0 讨论(0)
  • 2020-12-15 04:11

    For RxJava2 Use compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())

    For more information on usage https://github.com/JakeWharton/retrofit2-rxjava2-adapter

    0 讨论(0)
  • 2020-12-15 04:19

    From the said Github project page:

    Blockquote This is now DEPRECATED! Retrofit 2.2 and newer have a first-party call adapter for RxJava 2: https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2

    now you just need to include in your app/build.gradle file:

    compile 'com.squareup.retrofit2:adapter-rxjava2:latest.version'
    
    0 讨论(0)
  • 2020-12-15 04:20

    Be sure to add implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' or whatever version you are using to your dependencies, and then configure retrofit with that converter:

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

    Updated

    RxJavaCallAdapterFactory was renamed to RxJava2CallAdapterFactory. Changed the snipped above.

    0 讨论(0)
提交回复
热议问题