IllegalArgumentException:无法找到 rx.Observable RxJava、Retrofit2 的调用适配器
IllegalArgumentException: Could not locate call adapter for 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) {
}
});
请务必将 implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
或您正在使用的任何版本添加到您的依赖项中,然后使用该转换器配置改造:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(endpoint)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
已更新
RxJavaCallAdapterFactory
已重命名为 RxJava2CallAdapterFactory。更改了上面的片段。
对于 RxJava2 使用 compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
有关用法的更多信息https://github.com/JakeWharton/retrofit2-rxjava2-adapter
来自上述 Github 项目页面:
Blockquote
This is now DEPRECATED!
Retrofit 2.2 and newer have a first-party call adapter for 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())
你应该使用最新版本的所有 Rx 依赖项,这里我使用的是 2 版本(比如 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'
再补充一件事:
addCallAdapterFactory(RxJava2CallAdapterFactory.create())
在 Retrofit Api 客户端中
喜欢 :
retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build();
我在调用其余 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) {
}
});
请务必将 implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
或您正在使用的任何版本添加到您的依赖项中,然后使用该转换器配置改造:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(endpoint)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
已更新
RxJavaCallAdapterFactory
已重命名为 RxJava2CallAdapterFactory。更改了上面的片段。
对于 RxJava2 使用 compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
有关用法的更多信息https://github.com/JakeWharton/retrofit2-rxjava2-adapter
来自上述 Github 项目页面:
Blockquote This is now DEPRECATED! Retrofit 2.2 and newer have a first-party call adapter for 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())
你应该使用最新版本的所有 Rx 依赖项,这里我使用的是 2 版本(比如 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'
再补充一件事:
addCallAdapterFactory(RxJava2CallAdapterFactory.create())
在 Retrofit Api 客户端中 喜欢 :
retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build();