通过在改装中透明地发送另一个请求来处理特定错误
题
这是我尝试处理的情况,
- 如果执行请求,并且响应表示auth令牌已过期,
- 发送刷新令牌请求
- 如果刷新令牌请求成功,请重试原始请求
这应该是呼叫活动,片段......从来电者的角度来透明,它是一个请求和一个响应。
我在直接使用Okhttpclient之前实现了这一流程,但我不知道如何在改造中实现这一点。
也许是与此开放相关的东西问题关于一个responseInterceptor?
如果在改装中没有直接的方式实现这一目标,那么实现它的最佳方式是什么?基本侦听器类?
我也在使用Robosopit,如果在这种情况下它可能会有所帮助。
解决方案
由于我正在使用RobospiCe,我通过创建抽象的世代ocodeTagcode结束了这样做。
public abstract class BaseRequestListener<T> implements RequestListener<T> {
@Override
public void onRequestFailure(SpiceException spiceException) {
if (spiceException.getCause() instanceof RetrofitError) {
RetrofitError error = (RetrofitError) spiceException.getCause();
if (!error.isNetworkError()
&& (error.getResponse().getStatus() == INVALID_ACCESS_TOKEN_STATUS_CODE)) {
//I'm using EventBus to broadcast this event,
//this eliminates need for a Context
EventBus.getDefault().post(new Events.OnTokenExpiredEvent());
//You may wish to forward this error to your listeners as well,
//but I don't need that, so I'm returning here.
return;
}
}
onRequestError(spiceException);
}
public abstract void onRequestError(SpiceException spiceException);
}
. 不隶属于 StackOverflow