IllegalArgumentException: could not find a call adapter for rx.Observable RxJava, Retrofit2

I get the above error by calling the rest of the api. I use 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) { } }); 
+19
android rx-java retrofit2
source share
5 answers

Be sure to add the implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' or any implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' version that you use, in your dependencies, and then configure the modification with this converter:

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

updated

RxJavaCallAdapterFactory been renamed RxJava2CallAdapterFactory . Changed the cut above.

+40
source share

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

.addCallAdapterFactory (RxJava2CallAdapterFactory.create ())

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

+12
source share

You should use all the Rx dependencies of the latest version, here I am using version 2 (e.g. 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()) 

The Retrofit Api client, like:

 retrofit = new Retrofit.Builder() .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(BASE_URL) .build(); 
+5
source share

From the specified Github project page:

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

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

 compile 'com.squareup.retrofit2:adapter-rxjava2:latest.version' 
+1
source share

In my case, this was enough to replace

 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 

from

 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
0
source share

All Articles