当我想用retrofit和rxandroid重试请求时,如何修改请求参数和标题

时间:2018-02-23 08:10:09

标签: android retrofit okhttp rx-android

我想在我的订阅者中重试请求取决于我们的服务器给出的错误,但我需要在重试之前修改请求信息(标题和参数)。我可以这样做吗?

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

我想在订阅者的onFailed()方法中进行重试,请

1 个答案:

答案 0 :(得分:3)

你需要的是Interceptor(我假设你正在使用OkHttp和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);
    }
}