我希望全局捕获特定的状态代码,执行某些操作,然后在完成某些操作后重试该请求。
我写了一个拦截器,在收到状态代码后重试请求。
请求本身正在重试并且成功,但在这种情况下不会调用原始回调。
我需要添加什么来确保调用原始回调?
public class LoginResponseInterceptor implements Interceptor, SocketListener {
private Set<Chain> requestsToRetry = new HashSet<>();
@Override
public void onLoggedIn() {
List<Chain> requestsToResend = new ArrayList<>(requestsToRetry);
for (Chain chain : requestsToResend) {
try {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
String token = getToken();
if (token != null) {
Headers headers = request.headers().newBuilder().set("Authorization",token).build();
builder.headers(headers);
}
Request newRequest = builder.build();
chain.proceed(newRequest);
} catch (IOException e) {
e.printStackTrace();
}
}
chainSet.removeAll(requestsToResend);
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.code() == 401) {
chainSet.add(chain);
tryRelogin();
}
return response;
}
}