如果接口請(qǐng)求失敗,使用二進(jìn)制指數(shù)退避算法進(jìn)行接口重試畴蒲。
Observable
.create((ObservableEmitter<Object> emitter) -> {
System.out.println("開始請(qǐng)求接口...");
emitter.onError(new RuntimeException("請(qǐng)求接口異常..."));
})
.retryWhen((Observable<Throwable> errorObservable) -> errorObservable
.zipWith(Observable.just(1, 2, 4, 8, 16), (Throwable e, Integer time) -> time)
.flatMap((Integer delay) -> {
System.out.println(delay + "秒后重試");
return Observable.timer(delay, TimeUnit.SECONDS);
}))
.subscribe(System.out::println);