本文介绍了IllegalArgumentException:无法找到rx的呼叫适配器.可观察到的RxJava,Retrofit2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在调用rest api时遇到上述错误.我同时使用Retrofit2和RxJava.
I am getting the above error while calling the rest api. I am using both retrofit2 and RxJava.
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;
}
}
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);
}
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) {
}
});
推荐答案
请确保将implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
或您正在使用的任何版本添加到依赖项,然后使用该转换器配置改造:
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();
已更新
RxJavaCallAdapterFactory
重命名为 RxJava2CallAdapterFactory .更改了上面的片段.
RxJavaCallAdapterFactory
was renamed to RxJava2CallAdapterFactory. Changed the snipped above.
这篇关于IllegalArgumentException:无法找到rx的呼叫适配器.可观察到的RxJava,Retrofit2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!