修复在线搜索RxJava2

时间:2018-12-03 17:17:09

标签: java android error-handling rx-java2

我的服务器上有在线搜索产品,但是当连接不可用时,我需要本地搜索产品。我的代码不起作用:

private Flowable<SearchProducts> getRequest(Flowable<SearchProducts> network,Flowable<SearchProducts> local) {
    return network
            .retryWhen(new RetryWithDelay(Util.PING_COUNT,Util.PING_TIME))
            .onErrorResumeNext(local);
}
public class RetryWithDelay implements Function<Flowable<? extends Throwable>, Flowable<?>> {
private final int maxRetries;
private final int retryDelayMillis;
private int retryCount;

public RetryWithDelay(final int maxRetries, final int retryDelayMillis) {
    this.maxRetries = maxRetries;
    this.retryDelayMillis = retryDelayMillis;
    this.retryCount = 0;
}

@Override
public Flowable<?> apply(final Flowable<? extends Throwable> attempts) {
    return attempts
            .flatMap((Function<Throwable, Flowable<?>>) throwable -> {
                if (++retryCount < maxRetries) {
                    // When this Observable calls onNext, the original
                    // Observable will be retried (i.e. re-subscribed).
                    return Flowable.timer(retryDelayMillis,
                            TimeUnit.MILLISECONDS);
                }

                // Max retries hit. Just pass the error along.
                return Flowable.error(throwable);
            });
}

出现连接错误时,本地搜索不起作用。 请帮我,谢谢。

0 个答案:

没有答案