RxJava1 到 Rxjava2 迁移错误
RxJava1 to Rxjava2 Migration Error
Converting RxJava1 code to RXJava2 给出这个错误,不知道我做错了什么
不存在类型变量 R 的实例,因此 Single> 符合 ObserableSource
API 调用
@GET("/search/users?per_page=2")
Observable<UsersList> searchGithubUsers(@Query("q") String searchTerm);
@GET("/users/{username}")
Observable<User> getUser(@Path("username") String username);
RxJAva1:
public Observable<List<User>> searchUsers(final String searchTerm) {
return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap(
usersList -> Observable.from(usersList.getItems())
.concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList()))
.retryWhen(observable -> observable.flatMap(o -> {
if (o instanceof IOException) {
return Observable.just(null);
}
return Observable.error(o);
}));
}
RXJava2
public Observable<List<User>> searchUsers(final String searchTerm) {
return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap(
usersList -> Observable.fromIterable(usersList.getItems())
.concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList()))
.retryWhen(observable -> observable.flatMap(o -> {
if (o instanceof IOException) {
return Observable.just(null);
}
return Observable.error(o);
}));
}
尝试使用RxJava2CallAdapterFactory
:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
toList
returnsSingle
在2.x;你必须在它之后申请 toObservable()
。还要注意 Observable.just(null)
是被禁止的:RxJava 2 通常不允许空值。
Observable<List<User>> searchUsers(final String searchTerm) {
return Observable.defer(() ->
githubUserRestService
.searchGithubUsers(searchTerm)
.concatMap(usersList ->
Observable.fromIterable(usersList.getItems())
.concatMap(user ->
githubUserRestService
.getUser(user.getLogin()))
.toList()
.toObservable()
)
)
.retryWhen(observable ->
observable.flatMap(o -> {
if (o instanceof IOException) {
return Observable.just(0);
}
return Observable.error(o);
})
)
);
}
Converting RxJava1 code to RXJava2 给出这个错误,不知道我做错了什么
不存在类型变量 R 的实例,因此 Single> 符合 ObserableSource
API 调用
@GET("/search/users?per_page=2")
Observable<UsersList> searchGithubUsers(@Query("q") String searchTerm);
@GET("/users/{username}")
Observable<User> getUser(@Path("username") String username);
RxJAva1:
public Observable<List<User>> searchUsers(final String searchTerm) {
return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap(
usersList -> Observable.from(usersList.getItems())
.concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList()))
.retryWhen(observable -> observable.flatMap(o -> {
if (o instanceof IOException) {
return Observable.just(null);
}
return Observable.error(o);
}));
}
RXJava2
public Observable<List<User>> searchUsers(final String searchTerm) {
return Observable.defer(() -> githubUserRestService.searchGithubUsers(searchTerm).concatMap(
usersList -> Observable.fromIterable(usersList.getItems())
.concatMap(user -> githubUserRestService.getUser(user.getLogin())).toList()))
.retryWhen(observable -> observable.flatMap(o -> {
if (o instanceof IOException) {
return Observable.just(null);
}
return Observable.error(o);
}));
}
尝试使用RxJava2CallAdapterFactory
:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
toList
returnsSingle
在2.x;你必须在它之后申请 toObservable()
。还要注意 Observable.just(null)
是被禁止的:RxJava 2 通常不允许空值。
Observable<List<User>> searchUsers(final String searchTerm) {
return Observable.defer(() ->
githubUserRestService
.searchGithubUsers(searchTerm)
.concatMap(usersList ->
Observable.fromIterable(usersList.getItems())
.concatMap(user ->
githubUserRestService
.getUser(user.getLogin()))
.toList()
.toObservable()
)
)
.retryWhen(observable ->
observable.flatMap(o -> {
if (o instanceof IOException) {
return Observable.just(0);
}
return Observable.error(o);
})
)
);
}