这是我要处理的情况
这应该对调用方Activity,Fragment等透明。从调用方的角度来看,这是一个请求和一个响应。
在直接使用OkHttpClient之前,我已经实现了该流程,但是我不知道如何在Retrofit中实现这一点。
也许与这个关于ResponseInterceptor的公开问题有关的东西?
如果没有直接的方法来实现这一目标,那么实现它的最佳方法是什么?基础侦听器类?
我也将RoboSpice与Retrofit一起使用,如果在这种情况下可能会有所帮助。
由于我使用的是RoboSpice,因此最终通过创建abstract来完成此操作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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句