您好我正在观察我的服务器每15秒询问其在线/离线状态:
public Observable repeatCheckServerStatus(int intervalSec, final String path) {
return Observable.interval(intervalSec, TimeUnit.SECONDS)
.flatMap(new Func1>() {
@Override
public Observable call(Long aLong) {
return Observable.create(new Observable.OnSubscribe() {
@Override
public void call(Subscriber super Response> subscriber) {
try {
Response response = client.newCall(new Request.Builder()
.url(path + API_ACTION_CHECK_ONLINE_STATUS)
.header("Content-Type", "application/x-www-form-urlencoded")
.get()
.build()).execute();
subscriber.onNext(response);
subscriber.onCompleted();
if (!response.isSuccessful())
subscriber.onError(new Exception());
} catch (Exception e) {
subscriber.onError(e);
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
});
}
在我调用此方法之后,首先执行代码将在intervalSec时间之后(在我的情况下为15秒).查看区间方法的rxJava docummentation:
这应该是这样的.
问题:有没有办法立即执行代码然后重复间隔?