Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装及保养;okhttpclient截获401响应_Android_Retrofit_Okhttp - Fatal编程技术网

Android 改装及保养;okhttpclient截获401响应

Android 改装及保养;okhttpclient截获401响应,android,retrofit,okhttp,Android,Retrofit,Okhttp,我试图看到,每当我从我的API得到一个代码为401的响应时。 但当我这么做的时候,我会得到一个例外 @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); if (response.code() == 401) { m

我试图看到,每当我从我的API得到一个代码为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:连接{proxy=DIRECT@hostAddress=cipherSuite=none protocol=http/1.1}上的流意外结束(循环计数=0)

排队

        Response response = chain.proceed(request);

我应该如何使用401(未经授权的代码)获得响应以处理此问题?

我通常仅对请求使用inteceptors,并在rest适配器生成器上设置错误处理程序以处理错误,请参见下面的示例:

注意:
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;
                            }
                        }
                    })
val contentType = response.body?.contentType()
        val charset: Charset = contentType?.charset(StandardCharsets.UTF_8) ?: StandardCharsets.UTF_8
        val message= response.body?.source()?.readString(charset)