2

I'm using OkHttp with Retrofit to do synchronized requests. The problem is that OkHttp throws an exception. I can catch the exception in the interceptor instead, but the response is null.

I'd like to display messages to the user based on HTTP response codes.

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);
}

Client Interceptor

_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();
Gehrig
  • 73
  • 3
  • 8

2 Answers2

1

u can test this code , when the response isn't 2xx it will worked that u can get the code with 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();
Eidon
  • 36
  • 1
  • It doesn't reach the response.code() because it throws exception at okhttp3.Response response = chain.proceed(request); – Gehrig Sep 20 '16 at 14:37
0

You can use HttpResponse class

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

If you are using com.squareup.okhttp.Response then you can use the code() method.

Response httpResponse = client.newCall(request).execute(); 
httpResponse.code();
Ajay P. Prajapati
  • 1,973
  • 1
  • 17
  • 34
  • But, don't I need to do Interceptor.Chain.process(request) to execute the request in an interceptor? – Gehrig Sep 09 '16 at 21:29