如何获取HTTP响应错误代码执行OkHttp同步请求

时间:2016-09-09 18:00:40

标签: java android retrofit2 okhttp okhttp3

我使用OkHttp和Retrofit进行同步请求。问题是OkHttp引发异常。我可以在拦截器中捕获异常,但响应为null。

我想根据HTTP响应代码向用户显示消息。

Response<List<Employee>> owner = null;
Call<List<Employee>> webCall = getWebService().getDeviceOwner("tolower(LoginId) eq tolower(\'" + SharedData.AuthorizationUserName + "\')");
try {
    owner = webCall.execute();
    if(isHttpResponseSuccess(owner.code())) {
        Employee employee = owner.body().get(0);
    }
    else {
        Log.e(TAG_NAME, "GetDeviceOwner() call failed. Http code=" + owner.code() + "/nMessage=" + owner.message());
}
catch (Exception e) {

   //Some type of network error.  401, etc?  I have no clue.
   Log.e(TAG_NAME, "GetDeviceOwner() exception=" + e);
}

客户端拦截器

_okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(
                            new Interceptor() {
                                @Override
                                public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                                    Request request = chain.request().newBuilder()
                                            .addHeader("Content-Type", "Application/JSON")
                                            .addHeader("Authorization", "Basic " + new String(Base64.encode((SharedData.AuthorizationUserName + ":" + SharedData.AuthorizationPassword).getBytes(), Base64.NO_WRAP)))
                                            .removeHeader("charset")
                                            .build();
                                    okhttp3.Response response = chain.proceed(request);
                                    Log.d(TAG_NAME, "Response code="+ response.code());
                                    Log.d(TAG_NAME, "Response="+ response.toString());
                                    return response;
                                }
                            }).addInterceptor(logging).build();

2 个答案:

答案 0 :(得分:1)

你可以测试这段代码,当响应不是2xx时,你可以使用e.getMessage()获得代码;

_okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(
                            new Interceptor() {
                                @Override
                                public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                                    Request request = chain.request().newBuilder()
                                            .addHeader("Content-Type", "Application/JSON")
                                            .addHeader("Authorization", "Basic " + new String(Base64.encode((SharedData.AuthorizationUserName + ":" + SharedData.AuthorizationPassword).getBytes(), Base64.NO_WRAP)))
                                            .removeHeader("charset")
                                            .build();
                                    okhttp3.Response response = chain.proceed(request);
                                    if (response.code()/100!=2){
                                        throw new IOException(response.code()+"");
                                    }
                                    Log.d(TAG_NAME, "Response code="+ response.code());
                                    Log.d(TAG_NAME, "Response="+ response.toString());
                                    return response;
                                }
                            }).addInterceptor(logging).build();

答案 1 :(得分:0)

您可以使用HttpResponse

HttpResponse httpResponse = client.newCall(request).execute(); 
httpResponse.getStatusLine().getStatusCode();

如果您使用的是com.squareup.okhttp.Response,那么您可以使用code()方法。

Response httpResponse = client.newCall(request).execute(); 
httpResponse.code();
相关问题