Retrofit 支持 rx.Future/Task/Async/Single 吗?
Does Retrofit support rx.Future/Task/Async/Single?
我在 this article:
找到了很好的 RxJava 用法示例
Subscription subscription = Single.create(new Single.OnSubscribe() {
@Override
public void call(SingleSubscriber singleSubscriber) {
String value = longRunningOperation();
singleSubscriber.onSuccess(value);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(String value) {
// onSuccess
Snackbar.make(rootView, value, Snackbar.LENGTH_LONG).show();
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
// handle onError
}
});
但是因为我使用的是 Retrofit,所以我想创建 RetrofitService 并使用 Single class 将两个后端请求的结果合并到一个数据集中,如下所述:
When subscribing to a Single, there is only an onSuccess Action and an
onError action. The Single class has a different set of operators than
Observable, with several operators that allow for a mechanism of
converting a Single to an Observable. For example, using the
Single.mergeWith() operator, two or more Singles of the same type can
be merged together to create an Observable, emitting the results of
each Single to one Observable.
是否有可能实现这一目标(以及如何实现)?
是的,见Retrofit Adapters
仅适用于 Retrofit2.0
我在 this article:
找到了很好的 RxJava 用法示例Subscription subscription = Single.create(new Single.OnSubscribe() {
@Override
public void call(SingleSubscriber singleSubscriber) {
String value = longRunningOperation();
singleSubscriber.onSuccess(value);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(String value) {
// onSuccess
Snackbar.make(rootView, value, Snackbar.LENGTH_LONG).show();
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
// handle onError
}
});
但是因为我使用的是 Retrofit,所以我想创建 RetrofitService 并使用 Single class 将两个后端请求的结果合并到一个数据集中,如下所述:
When subscribing to a Single, there is only an onSuccess Action and an onError action. The Single class has a different set of operators than Observable, with several operators that allow for a mechanism of converting a Single to an Observable. For example, using the Single.mergeWith() operator, two or more Singles of the same type can be merged together to create an Observable, emitting the results of each Single to one Observable.
是否有可能实现这一目标(以及如何实现)?
是的,见Retrofit Adapters 仅适用于 Retrofit2.0