有没有办法拦截HttpClient请求并调用其他请求,然后重新调用第一个请求

时间:2020-06-13 17:05:31

标签: android httpclient interceptor

我正在尝试处理刷新令牌,因此在我的拦截器中,我检查响应代码,如果它是401,则我继续进行刷新令牌请求

public class AuthInterceptor implements Interceptor {

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
    Request request = chain.request();
    AtomicReference<Response> response = new AtomicReference<>(chain.proceed(request));
    if (response.get().code() == HttpURLConnection.HTTP_UNAUTHORIZED && response.get().message().equals("Unauthorized")) {
        request = request.newBuilder().url(Url.REFRESH_TOKEN).build();
        response.set(chain.proceed(request));
        if (response.get().code() == 200){

        }else if (response.get().code() == 401){
            Log.e("request", "request failed");
        }
    }
    return response.get();
}

}

但是我的日志显示该com.ogram.sp E/failure: java.lang.IllegalArgumentException: method POST must have a request body.

所以它需要一个男孩,所以我该如何打电话截取一个请求并打电话给另一个

我正在使用android快速联网进行api调用和RXJava

0 个答案:

没有答案