17

I'm using a android-priority-jobqueue and I use retrofit to make synchronous calls to my rest api but i'm unsure how to handle errors like 401 Unauthorized errors which I send back json stating the error. Simple when doing async calls but I'm adapting my app for job manager. below is a simple try catch for IO exceptions, but 401's 422's etc? How to do this?

try {
    PostService postService = ServiceGenerator.createService(PostService.class);
    final Call<Post> call = postService.addPost(post);
    Post newPost = call.execute().body();

    // omitted code here

} catch (IOException e) {
    // handle error
}

EDIT

The use of the retrofit response object was the clincher for me, returning the retrofit response object allowed me to

Response<Post> response = call.execute();

if (response.isSuccessful()) {
    // request successful (status code 200, 201)
    Post result = response.body();

    // publish the post added event
    EventBus.getDefault().post(new PostAddedEvent(result));
} else {
    // request not successful (like 400,401,403 etc and 5xx)
    renderApiError(response);
}

2 Answers 2

11

Check response code and show the appropriate message.

Try this:

 PostService postService = ServiceGenerator.createService(PostService.class);
 final Call<Post> call = postService.addPost(post);

Response<Post> newPostResponse = call.execute();

// Here call newPostResponse.code() to get response code
int statusCode = newPostResponse.code();
if(statusCode == 200)
    Post newPost = newPostResponse.body();
else if(statusCode == 401)
    // Do some thing... 
6
  • Thank you, using the response object was what I needed, cheers :)
    – CaptRisky
    Sep 7, 2016 at 3:26
  • Glad to help you.
    – Faraz
    Sep 7, 2016 at 6:17
  • Doing the same I get an java.lang.NumberFormatException: Invalid double: "" error because the call.execute returns a json with empty values. I would like to get the body of the call.execute() and check it before doing anything else. When I write something like "Response<Post> newPostResponse = call.execute();" I do get an error as the body is not a correct <Post> Dec 13, 2016 at 16:47
  • 1
    @MaximeClaude Check my answer here to get the response in String
    – Faraz
    Dec 14, 2016 at 8:55
  • That is elementary. A call without handle possible http-errors is absolutely useless. I wonder why approaches without such an error-handling are even discussed in retrofit2-tutorials! Thank you :)
    – Jan
    Jan 4, 2017 at 12:17
1

Putting checks for 401 on every response is not a very good approach. Instead one can apply this check at the base level i.e. while creating an object for Retrofit, through the interceptors. Have a look:

public synchronized static Retrofit getClientWithRetry(final Context ctx) {
    if (clientWithRetry == null) {
        Interceptor responseCodeInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Response response = chain.proceed(request);
                if (response.code() == 401) {
                    Log.d(LOG_TAG, "Intercepted Req: " + response.toString());
                    Response r = retryWithFreshToken(request, chain);
                    return r;
                }
                return response;
            }
        };

        int cacheSize = 10 * 1024 * 1024; // 10 MB
        Cache cache = new Cache(ctx.getCacheDir(), cacheSize);

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(responseCodeInterceptor)
                .cache(cache)
                .build();

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client);
        clientWithRetry = builder.build();
    }
    return clientWithRetry;
}

Here internally if a 401 is observed a new chained request can be made and token can be fetched. Post which the original request can be completed. Taken from this Retrofit retry tutorial.

1
  • How do you handle timeout exceptions? I don't like to handle it for every request. Oct 13, 2020 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.