如何调用依赖于 rx 网络调用的非 rx 网络调用
How to call a non-rx network call that depends on an rx network call
我有一个 return 是 Observable
的网络调用,我还有另一个网络调用,它不是依赖于第一个 Observable
的 rx,我需要以某种方式用 Rx 全部转换。
Observable<Response> responseObservable = apiclient.executeRequest(request);
执行后我需要做另一个 http 调用,它不 return Observable
:
responseObservable.map(response - > execute the no rx network call using the response.id)
noRxClient.getInformation(response.id, new Action1<Information>() {
@Override
public void call(Information information) {
//Need to return information with page response
}
});
然后我需要调用此方法来呈现响应
renderResponse(response, information);
如何将非 rx 调用与 rx 连接,然后使用 RxJava 调用渲染响应?
您可以使用 Observable.fromEmitter
(RxJava1) 或 Observable.create
(RxJava2) 和 Observable.fromCallable
(对于 non-async 调用):
private Observable<Information> wrapGetInformation(String responseId) {
return Observable.create(emitter -> {
noRxClient.getInformation(responseId, new Action1<Information>() {
@Override
public void call(Information information) {
emitter.onNext(information);
emitter.onComplete();
//also wrap exceptions into emitter.onError(Throwable)
}
});
});
}
private Observalbe<RenderedResponse> wrapRenderResponse(Response response, Information information) {
return Observable.fromCallable(() -> {
return renderResponse(response, information);
//exceptions automatically wrapped
});
}
并使用 运算符组合结果:
apiclient.executeRequest(request)
.flatMap(response -> wrapGetInformation(response.id),
(response, information) -> wrapRenderResponse(response, information))
)
//apply Schedulers
.subscribe(...)
我有一个 return 是 Observable
的网络调用,我还有另一个网络调用,它不是依赖于第一个 Observable
的 rx,我需要以某种方式用 Rx 全部转换。
Observable<Response> responseObservable = apiclient.executeRequest(request);
执行后我需要做另一个 http 调用,它不 return Observable
:
responseObservable.map(response - > execute the no rx network call using the response.id)
noRxClient.getInformation(response.id, new Action1<Information>() {
@Override
public void call(Information information) {
//Need to return information with page response
}
});
然后我需要调用此方法来呈现响应
renderResponse(response, information);
如何将非 rx 调用与 rx 连接,然后使用 RxJava 调用渲染响应?
您可以使用 Observable.fromEmitter
(RxJava1) 或 Observable.create
(RxJava2) 和 Observable.fromCallable
(对于 non-async 调用):
private Observable<Information> wrapGetInformation(String responseId) {
return Observable.create(emitter -> {
noRxClient.getInformation(responseId, new Action1<Information>() {
@Override
public void call(Information information) {
emitter.onNext(information);
emitter.onComplete();
//also wrap exceptions into emitter.onError(Throwable)
}
});
});
}
private Observalbe<RenderedResponse> wrapRenderResponse(Response response, Information information) {
return Observable.fromCallable(() -> {
return renderResponse(response, information);
//exceptions automatically wrapped
});
}
并使用
apiclient.executeRequest(request)
.flatMap(response -> wrapGetInformation(response.id),
(response, information) -> wrapRenderResponse(response, information))
)
//apply Schedulers
.subscribe(...)