http code 202 :
The request has been accepted for processing, but the processing has not been completed.
這時(shí)候磕诊,服務(wù)器給你的body是空的党饮,如果你使用去解析為json珍手,那么,恭喜你
java.io.EOFException: End of input at line 1 column 1
在前面等著你
一個(gè)比較好的理解就是,服務(wù)器接受了你的請(qǐng)求扫夜,但是現(xiàn)在來(lái)不及處理惋戏,需要你等會(huì)試試。對(duì)于這種狀態(tài)碼贫奠,OkHttp并沒(méi)有為我們處理唬血,jake大神也回答如是:
這個(gè)issue在這里:
https://github.com/square/retrofit/issues/1554
我個(gè)人的解決辦法是如此:
- 寫一個(gè)攔截器,攔截這種202的狀態(tài)碼.
- 然后我們本身又使用的
rxjava
這個(gè)強(qiáng)大的框架去配合Retrofit
唤崭,這時(shí)候拷恨,我遇到這種狀態(tài)嗎的是時(shí)候,去重試即可谢肾,具體的攔截器如下:
public class CreateInterceptor implements Interceptor {
public static final int HTTP_CODE_ACCEPT = 202; //請(qǐng)求成功腕侄,但沒(méi)有處理
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request()); //如果401了,會(huì)先執(zhí)行TokenAuthenticator
Logger.e("CreateInterceptor request url "+response.request().url());
Logger.e("CreateInterceptor response code "+response.code());
if (response.code() == HTTP_CODE_ACCEPT) {
CreateInterceptorExceptioin interceptorExceptioin = new CreateInterceptorExceptioin();
interceptorExceptioin.setErrorCode(HTTP_CODE_ACCEPT);
interceptorExceptioin.setRetry_after(response.header("Retry-After"));
throw interceptorExceptioin;
}
return response;
}
public class CreateInterceptorExceptioin extends Error{
private int errorCode;
private String retry_after;
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getRetry_after() {
return retry_after;
}
public void setRetry_after(String retry_after) {
this.retry_after = retry_after;
}
}
}
然后芦疏,在build的時(shí)候
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new CreateInterceptor());//攔截202冕杠,直接返回錯(cuò)誤哦
OkHttpClient okHttpClient = builder.build();
再將上述 build出來(lái)的OkHttpClient
設(shè)置給 Retrofit
即可。
new Retrofit.Builder().client(....)...
那么酸茴,最最重要的分预,如何去重試呢?
實(shí)際上弊决,我也是參考了網(wǎng)上的一個(gè)例子 噪舀,在我的另外一篇文章中有提到這個(gè)技巧:android中使用Rxjava一些有意思的代碼集合 ,在第17條那里飘诗,參照這個(gè)原理与倡,略微改了改,這里的代碼如下:
public static final class RetryWhen202Happen implements Func1<Observable<? extends Throwable>, Observable<?>> {
private final int _maxRetries;
private final int _retryDelayMillis;
private int _retryCount;
public RetryWhen202Happen(final int maxRetries, final int retryDelayMillis) {
_maxRetries = maxRetries;
_retryDelayMillis = retryDelayMillis;
_retryCount = 0;
}
@Override
public Observable<?> call(Observable<? extends Throwable> inputObservable) {
return inputObservable.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(Throwable throwable) {
if (++_retryCount < _maxRetries && throwable instanceof CreateInterceptor.CreateInterceptorExceptioin) {
// When this Observable calls onNext, the original
// Observable will be retried (i.e. re-subscribed)
Log.e("RetryWhen202Happen", "call: "+_retryCount);
return Observable.timer(_retryCount * _retryDelayMillis, TimeUnit.MILLISECONDS);
}
return Observable.error(throwable);
}
});
}
}
最后:我們使用rxjava
提供的retryWhen
即可昆稿。