gpt4 book ai didi

java - RxJava2、Retrofit - doOnNext、doAfterNext、doOnComplete 不调用

转载 作者:行者123 更新时间:2023-11-30 06:33:54 24 4
gpt4 key购买 nike

我尝试从服务器获取一些数据。

Observable<List<Countries>> backendObservable = mCountriesApi.getCountries()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());

backendObservable.doOnNext(list -> Log.d(TAG, "doOnNext"));
backendObservable.doAfterNext(list -> Log.d(TAG, "doAfterNext"));
backendObservable.doOnComplete(() -> Log.d(TAG, "doOnComplete"));

backendObservable.subscribe(new Observer<List<Country>>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(List<Country> value) {
Log.d(TAG, "doOnNext"));
}

@Override
public void onError(Throwable e) {

}

@Override
public void onComplete() {

}
});

它有效,我将数据接收到“onNext”中。但 doOnNext、doAfterNext、doOnComplete 方法不会调用。我做错了什么?

最佳答案

直到 observeOn 之前你都知道了,发生了什么?您必须在 Observable 上链接运算符,因为它们是不可变的数据流计划:

backendObservable = backendObservable.doOnNext(list -> Log.d(TAG, "doOnNext"));
backendObservable = backendObservable.doAfterNext(list -> Log.d(TAG, "doAfterNext"));
backendObservable = backendObservable.doOnComplete(() -> Log.d(TAG, "doOnComplete"));

或者更简洁

Observable<List<Countries>> backendObservable = mCountriesApi.getCountries()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(list -> Log.d(TAG, "doOnNext"));
.doAfterNext(list -> Log.d(TAG, "doAfterNext"));
.doOnComplete(() -> Log.d(TAG, "doOnComplete"));
.subscribe(...);

关于java - RxJava2、Retrofit - doOnNext、doAfterNext、doOnComplete 不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45546591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com