jason.yu
jason.yu

Reputation: 59

How to modify the request params and headers when i want to retry the request with retrofit and rxandroid

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

Upvotes: 0

Views: 376

Answers (1)

artkoenig
artkoenig

Reputation: 7257

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);
    }
}

Upvotes: 3

Related Questions