Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

android - 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) {

                }
            });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...