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();
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); }
android error-handling synchronous retrofit2
Captiskisk
source share