这是我正在尝试处理的案例,
- 如果执行请求,并且响应指示身份验证令牌已过期,
- 发送刷新令牌请求
- 如果刷新令牌请求成功,则重试原始请求
这对于调用 Activity、Fragment...等应该是透明的。从调用者的角度来看,这是一个请求,一个响应。
我之前直接使用OkHttpClient时已经实现了这个流程,但我不知道如何在Retrofit中实现这个流程。
也许与此开放相关issue https://github.com/square/retrofit/issues/261关于响应拦截器?
如果在改造中没有直接的方法来实现这一目标,那么实施它的最佳方法是什么?基础侦听器类?
我也在使用 RoboSpice 和 Retrofit,如果它在这种情况下有帮助的话。
由于我使用的是 RoboSpice,因此我最终通过创建一个摘要来完成此操作BaseRequestListener
.
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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)