【发布时间】:2017-07-05 08:27:57
【问题描述】:
我希望在预定义的定期间隔内对后端调用进行一定次数的轮询。如果我在循环之间收到预期的有效负载,我想退出循环并更新 UI,否则终止轮询。
以下是我进行标准 http 调用时通常执行的代码。
//Response Model from backend API
public class ApplicationStatusResponse
{
public boolean isActive;
}
//Retrofit facade
@POST(v1/api/applicationStatus)
Single<ApplicationStatusResponse> checkApplicationStatus(@Body ApplicationStatusRequest applicationRequest);
-----
DisposableSingleObserver<ApplicationStatusResponse> disposableSingleObserver = new DisposableSingleObserver<ApplicationStatusResponse>() {
@Override
public void onSuccess(ApplicationStatusResponse response) {
// Update UI Here
}
@Override
public void onError(Throwable e) {
}
};
CompositeDisposable compositeDisposable = new CompositeDisposable();
// Following call works alaways works
DisposableSingleObserver<ApplicationStatusResponse> disposable = originationRepo.checkApplicationStatus(applicationStatusRequest)
.observeOn(schedulerProvider.mainThread())
.subscribeWith(disposableSingleObserver);
compositeDisposable.add(disposable);
但是我在下面的代码中有点迷失了,语法错误,当从 Flowable.interval 调用时我无法使用相同的disposableSingleObserver,并且在我需要更新 UI 的用例方面需要帮助定期状态直到时间过去或状态处于活动状态,这首先发生,如果我收到 500 的 HTTP 状态代码,我也不会终止轮询,而是重复直到满足上述条件。
//Help Needed here when I need polling in regular interval - I am kind of the syntax error complain from Android Studio
int INITIAL_DELAY = 0;
int POLLING_INTERVAL = 1000;
int POLL_COUNT = 8;
disposable = Flowable
.interval(INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS)
.map(x -> originationRepo.checkApplicationStatus(applicationStatusRequest))
.take(POLL_COUNT) ??
// How can I receive the response payload and update the UI
compositeDisposable.add(disposable);
提前感谢您的帮助。
【问题讨论】:
标签: android retrofit2 rx-java2