0

I want to retry the request in my subscriber depends on the error which our server gave, but I need to modify the request info(headers and params) before retrying.how can I do?

ServerApi.login("103", "json", "379020", "银魂", "6")
         .subscribe(new DialogSubscriber<String>(this, true) {

    @Override
    protected void onCCSuccess(String data) {
        Toast.makeText(mActivity, "success", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onFailed(int code, String message) {
        if(code == RETRY_CODE){
            retry();//modify this request params and headers and resend this request again 
        }else{
            super.onFailed(code, message);
        }
    }
});

i want to do the retry in the subscriber's onFailed() method,please

Banana
  • 2,435
  • 7
  • 34
  • 60
jason.yu
  • 59
  • 5
  • FYI (not sure if it may help): retrofit/okhttp provides a *retry policy*, you can implement as an interceptor. Find further info here: https://stackoverflow.com/questions/24562716/how-to-retry-http-requests-with-okhttp-retrofit – BenRoob Feb 23 '18 at 08:41

1 Answers1

3

What you need is an Interceptor (I assume, you are using OkHttp with Retrofit).

@Override
public Response intercept(Chain chain) throws IOException {
    okhttp3.Request original = chain.request();    
    Response origResponse = chain.proceed(request);
    if (origResponse.code() == RETRY_CODE) {
        // modify your original request (add headers to it etc.)
        ...
        return chain.proceed(original);
    }
}
artkoenig
  • 7,117
  • 2
  • 40
  • 61
  • More a comment, as an answer. – BenRoob Feb 23 '18 at 08:41
  • no :) But I think "answers" that simply link to documentations / resources, without explanation should be a comment. Nothing personal!!! In addition, we assume usage of OkHttp, but author has not confirmed it. Okay, 99% it should be OkHttp ;) Hope you get my point. Thanks for editing your answer. – BenRoob Feb 23 '18 at 09:16
  • Thanks!This is exactly what i need! – jason.yu Feb 26 '18 at 05:18