4
我想看到每当我从我的API代码为401的响应 但是,当我这样做,我得到一个IOException改造及okhttpclient拦截401响应
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.code() == 401) {
mLoginToken.delete();
Toast.makeText(mApplication.getApplicationContext(), R.string.session_error, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mApplication.getApplicationContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mApplication.startActivity(intent);
}
return response;
}
我会得到错误 java.io.IOException的:在连接流意外结束{代理= DIRECT @ hostAddress =密码组=无协议= HTTP/1.1}(再循环计数= 0)
在线路
Response response = chain.proceed(request);
为了处理这个问题,我应该如何获得401(未授权代码)的答复?
有没有其他的默认错误处理程序,如果我设置了新的错误处理程序,会有一些未来的问题? –
如果我没有弄错,设置另一个错误处理程序将会覆盖之前设置的错误处理程序,并且我不确定您希望错误处理有多复杂,您总是可以将“case”代码块抽象为静态函数来维护在您的代码中的可再读性 –
感谢您分享此解决方案。我喜欢 ;) –