Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

ServiceFactory.java

public class ServiceFactory {
public static <T> T createRetrofitService(final Class<T> clazz, final String endpoint){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(endpoint)
            //.addConverterFactory(GsonConverterFactory.create())

            .build();

    T service = retrofit.create(clazz);
    return service;
}

}

MovieService.java

public interface MovieService{
//public final String API_KEY = "<apikey>";
public final String SERVICE_END = "https://api.mymovies.org/3/";
@GET("movie/{movieId}??api_key=xyz")
Observable<Response<Movies>> getMovies(@Field("movieId") int movieId);

}

Inside MainActivity

      MovieService   tmdbService = ServiceFactory.createRetrofitService(MovieService.class, MovieService.SERVICE_END);
    Observable<Response<Movies>> responseObservable = tmdbService.getMovies(400);
    responseObservable .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Response<Movies>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Response<Movies> moviesResponse) {

                }
            });
like image 579
Hemendra Sethi Avatar asked Feb 05 '16 20:02

Hemendra Sethi


3 Answers

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.

like image 69
Logain Avatar answered Oct 19 '22 15:10

Logain


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

like image 15
Etukeni E. Ndecha O Avatar answered Oct 19 '22 17:10

Etukeni E. Ndecha O


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'

enter image description here

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();
like image 13
Geet Thakur Avatar answered Oct 19 '22 15:10

Geet Thakur