IllegalArgumentException:找不到rx.Observable RxJava调用适配器,Retrofit2

我在调用其余api时遇到了上述错误。 我正在使用retrofit2和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); 

}

在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) { } }); 

Solutions Collecting From Web of "IllegalArgumentException:找不到rx.Observable RxJava调用适配器,Retrofit2"

一定要添加compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'或者你正在使用的任何testing版本到你的依赖关系,然后configuration该转换器的改进:

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

对于RxJava2使用compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

有关使用的更多信息https://github.com/JakeWharton/retrofit2-rxjava2-adapter

从上述Github项目页面:

Blockquote现在已经被取消了! 2.2和更新版本的RxJava 2有一个第一方调用适配器: https : //github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2

现在你只需要在你的app / build.gradle文件中包含:

 compile 'com.squareup.retrofit2:adapter-rxjava2:latest.version' 

就我而言,这足以取代

 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 

 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())