Modification of 2 synchronous error handling for 4xx errors

I use android-priority-jobqueue and I use modification to make synchronous calls for my rest api, but I am not sure how to handle errors such as 401 Unauthorized errors that I send back to json indicating the error. Simple when making asynchronous calls, but I am adapting my application to the task manager. below is a simple catch attempt for IO exceptions, but 401 422 etc.? How to do it?

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

Using the retrograde response object was the key for me, returning a retrofit response object that allowed me

 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); } 
+7
android error-handling synchronous retrofit2
source share
1 answer

Check the response code and show the corresponding message.

Try the following:

  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... 
+5
source share

All Articles