Bol*_*aul 5 android retrofit okhttp
我试图看到每当我从我的API获得代码401的响应.但是当我这样做时,我得到一个IOException
@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;
}
Run Code Online (Sandbox Code Playgroud)
我将得到错误java.io.IOException:连接上的意外结束流{proxy = DIRECT @ hostAddress = cipherSuite = none protocol = http/1.1}(recycle count = 0)
在线
Response response = chain.proceed(request);
Run Code Online (Sandbox Code Playgroud)
我应该如何获得401(未授权代码)的响应才能处理这个问题?
我通常只将请求程序用于请求,并且处理错误会在其余适配器构建器上设置错误处理程序,请参阅下面的示例:
注意:
cause.getResponse()可能会返回null
yourRestAdapterBuilder.setErrorHandler(new ErrorHandler() {
@Override
public Throwable handleError(RetrofitError cause) {
switch (cause.getResponse().getStatus()) {
case 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);
default:
// suppress or handle other errors
break;
}
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5757 次 |
| 最近记录: |