OkHttp自定义重试次数
public class RetryInterceptor implements Interceptor {
private static final String TAG = "RetryInterceptor";
private int maxRetry = 3;
private long delay = 3000;
private long increaseDelay = 5000;
public RetryInterceptor() {
}
public RetryInterceptor(int maxRetry) {
this.maxRetry = maxRetry;
}
public RetryInterceptor(int maxRetry, long delay) {
this.maxRetry = maxRetry;
this.delay = delay;
}
public RetryInterceptor(int maxRetry, long delay, long increaseDelay) {
this.maxRetry = maxRetry;
this.delay = delay;
this.increaseDelay = increaseDelay;
}
@Override
public Response intercept(Chain chain) throws IOException {
RetryWrapper retryWrapper = proceed(chain);
while (retryWrapper.isNeedReTry()) {
retryWrapper.retryNum++;
Logcat.d().tag(TAG_BAODA).tag(TAG).format("url= %s", retryWrapper.request.url().toString()).msg("retryNum= " + retryWrapper.retryNum).out();
try {
Thread.sleep(delay + (retryWrapper.retryNum - 1) * increaseDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
proceed(chain, retryWrapper.request, retryWrapper);
}
return retryWrapper.response == null ? chain.proceed(chain.request()) : retryWrapper.response;
}
private RetryWrapper proceed(Chain chain) throws IOException {
Request request = chain.request();
RetryWrapper retryWrapper = new RetryWrapper(request, maxRetry);
proceed(chain, request, retryWrapper);
return retryWrapper;
}
private void proceed(Chain chain, Request request, RetryWrapper retryWrapper) throws IOException {
try {
Response response = chain.proceed(request);
retryWrapper.setResponse(response);
} catch (SocketException | SocketTimeoutException e) {
}
}
static class RetryWrapper {
volatile int retryNum = 0;
Request request;
Response response;
private int maxRetry;
public RetryWrapper(Request request, int maxRetry) {
this.request = request;
this.maxRetry = maxRetry;
}
public void setResponse(Response response) {
this.response = response;
}
Response response() {
return this.response;
}
Request request() {
return this.request;
}
public boolean isSuccessful() {
return response != null && response.isSuccessful();
}
public boolean isNeedReTry() {
return !isSuccessful() && retryNum < maxRetry;
}
public void setRetryNum(int retryNum) {
this.retryNum = retryNum;
}
public void setMaxRetry(int maxRetry) {
this.maxRetry = maxRetry;
}
}
}
RxJava 实现
public class RetryWhenNetworkException implements Function<Observable<? extends Throwable>, Observable<?>> {
private int count = 3;
private long delay = 5000;
private long increaseDelay = 5000;
public RetryWhenNetworkException() {
}
public RetryWhenNetworkException(int count, long delay) {
this.count = count;
this.delay = delay;
}
public RetryWhenNetworkException(int count, long delay, long increaseDelay) {
this.count = count;
this.delay = delay;
this.increaseDelay = increaseDelay;
}
@Override
public Observable<?> apply(Observable<? extends Throwable> observable) throws Exception {
return observable
.zipWith(Observable.range(1, count + 1), new BiFunction<Throwable, Integer, Wrapper>() {
@Override
public Wrapper apply(Throwable throwable, Integer integer) {
return new Wrapper(throwable, integer);
}
}).flatMap(new Function<Wrapper, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(Wrapper wrapper) throws Exception {
if ((wrapper.throwable instanceof ConnectException
|| wrapper.throwable instanceof SocketTimeoutException
|| wrapper.throwable instanceof TimeoutException
|| wrapper.throwable instanceof HttpException)
&& wrapper.index < count + 1) {
Log.e("tag", "retry---->" + wrapper.index);
return Observable.timer(delay + (wrapper.index - 1) * increaseDelay, TimeUnit.MILLISECONDS);
}
return Observable.error(wrapper.throwable);
}
});
}
private class Wrapper {
private int index;
private Throwable throwable;
Wrapper(Throwable throwable, int index) {
this.index = index;
this.throwable = throwable;
}
}
}
完美Commit