来自后端的X-Error

时间:2017-11-24 13:44:18

标签: android okhttp

我想解析错误。 在向后端发送请求后,我得到了回复:

11-24 14:23:35.553 22759-26071/eu.incare.android D/OkHttp: Date: Fri, 24 Nov 2017 13:23:35 GMT
11-24 14:23:35.554 22759-26071/eu.incare.android D/OkHttp: Server: Apache
11-24 14:23:35.554 22759-26071/eu.incare.android D/OkHttp: Cache-Control: no-cache
11-24 14:23:35.555 22759-26071/eu.incare.android D/OkHttp: X-Runtime: 0.010097
11-24 14:23:35.555 22759-26071/eu.incare.android D/OkHttp: X-Frame-Options: SAMEORIGIN
11-24 14:23:35.556 22759-26071/eu.incare.android D/OkHttp: X-XSS-Protection: 1; mode=block
11-24 14:23:35.556 22759-26071/eu.incare.android D/OkHttp: X-Content-Type-Options: nosniff
11-24 14:23:35.557 22759-26071/eu.incare.android D/OkHttp: X-Error: This day is not in the range of tracking days
11-24 14:23:35.557 22759-26071/eu.incare.android D/OkHttp: X-Request-Id: dab1d774-257e-4f05-8ceb-48c61b8a0254
11-24 14:23:35.557 22759-26071/eu.incare.android D/OkHttp: X-Powered-By: Phusion Passenger 5.1.11
11-24 14:23:35.558 22759-26071/eu.incare.android D/OkHttp: Status: 404 Not Found
11-24 14:23:35.558 22759-26071/eu.incare.android D/OkHttp: Content-Length: 0
11-24 14:23:35.559 22759-26071/eu.incare.android D/OkHttp: Keep-Alive: timeout=5, max=99
11-24 14:23:35.559 22759-26071/eu.incare.android D/OkHttp: Connection: Keep-Alive
11-24 14:23:35.560 22759-26071/eu.incare.android D/OkHttp: Content-Type: application/json
11-24 14:23:35.564 22759-26071/eu.incare.android D/OkHttp: <-- END HTTP (0-byte body)

这是我的错误解析器:

 public void parseNetworkError(Throwable t) {

        if (t instanceof UnknownHostException) {
            getView().showError(R.string.error_message_no_wifi);
            return;
        }

        if (t instanceof SocketTimeoutException || t instanceof SocketException) {
            getView().showError(R.string.error_message_timeout);
            return;
        }

        if (t instanceof IOException) {
            getView().showError(R.string.error_message_connection_error);
            return;
        }

        if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            try {
                String jsonError = httpException.response().errorBody().source().readUtf8();
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(ApiException.class, new ApiExceptionTypeAdapter())
                        .create();
                ApiException apiException = gson.fromJson(jsonError, ApiException.class);

                if (apiException == null) {
                    apiException = new ApiException();
                }

                if (apiException.getErrorMessage() == 0) {
                    if (httpException.code() == 500) {
                        getView().showError(R.string.error_message_server_error);
                        return;
                    } else {
                        getView().showError(R.string.error_message_unexpected_api_error);
                        return;
                    }
                }
                getView().showError(apiException.getErrorMessage());
            } catch (IOException | JsonSyntaxException e) {
                e.printStackTrace();
                getView().showError(R.string.error_message_unexpected_api_error);
            } catch(Exception ex){
                ex.printStackTrace();
                getView().showError(R.string.error_message_unknown);
            }
        } else {
            getView().showError(R.string.error_message_unknown);
        }
    }

但是在Throwable中我看不到X-Error消息。我怎么能得到这个?我使用OkHttp并进行改造。

1 个答案:

答案 0 :(得分:2)

OkHttp Response有一个headers()方法,允许您访问响应标头。您的httpExceptionresponse()返回Response,因此您应该可以在那里找到X-Error标题。