How to configure Retrofit to convert success/error responses

Related searches

Server sends response in case of success (code 2xx):

{
    "user":"User",
    "age":16
}

And in case of fail (4xx) the following Json is sent in errorBody:

{
    "errorMessage":"Server is not working",
    "action":{
         ...
    }
}

How can I can I configure Retrofit so that it automatically converts responses based on success/fail. Note: I am using JavaRx v2.2.0 and Retrofit v2.4.0

My method to make API call is like:

@GET("/screens/home")
Single<HomeResponse> getHome(@Query("langCode") String langCode);

I tried to encapsulate HomeResponse (my own java class) with Response (Retrofit's). However, I did not get I want.

@GET("/screens/home")
Single<Reponse<HomeResponse>> getHome(@Query("langCode") String langCode);

Make your HomeResponse class with all property which you want from success and error like below.

class HomeResponse {
    String user;
    String age;
    String errorMessage;
    Action action;
}

Action will be your another class. You just check of errorMessage null or not at a time of response.

Retrofit 2 — Simple Error Handling, Let's assume your API sends a JSON error body like this: And the response converter is available via our Retrofit object. Retrofit 2 has a different concept of handling "successful" requests than Retrofit 1. Don't forget to add your model classes as exception to your ProGuard configuration as well. In our� Update: Handling with Retrofit's call adapter factory This is the way most apps that use RxJava solve this issue instead of wrapping every network call with a method. This is usually also the most

you can make pojo class using this link, write both success & error response fields in json which you are getting merge both json and make a pojo class

you can check the response by status or something which define that this is error response

EDIT

see this

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class MyApiResponse {

@SerializedName("user")
@Expose
private String user;
@SerializedName("age")
@Expose
private Integer age;
@SerializedName("errorMessage")
@Expose
private String errorMessage;
@SerializedName("action")
@Expose
private Action action;

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public Action getAction() {
return action;
}

public void setAction(Action action) {
this.action = action;
}

}

Retrofit 2 — Catch Server Errors Globally with Response Interceptor, Retrofit 2 — Catch Server Errors Globally with Response Interceptor When we introduced our API error handler we threw all possible non-success responses from the Response Interceptor; How to Detect Network and Conversion Errors in Make sure you are not configuring conflicting app behavior. All modern Android apps need to do network requests. Retrofit offers you an extremely convenient way of creating and managing network requests.From asynchronous execution on a background thread, to automatic conversion of server responses to Java objects, Retrofit does almost everything for you.

For this form of response data rules, it's better to create a base response class:

class BaseResponse {
    String errorMessage;
    Action action;
    //ingore getters setters
}

And make your other response classes extend it

class HomeResponse extend BaseResponse{
    String user;
    String age;
}

class OtherResp extend BaseResponse{
    String otherField;
}

Android - Retrofit 2 Custom Error Response Handling, How to parse custom error body response using Retrofit 2 Android HTTP Client. Usually a response is considered success if the status scode is 2xx and Retrofit has If you have different response body format, you can adjust it to suit your case. Finally, change the onResponse and onFailure methods. When we make any bad request (HTTP 400) Retrofit doesn’t convert the response. In this case we can access the raw response with response.errorBody.string(). For better understanding see the code

Using Retrofit 2.x as REST client - Tutorial, Response; import retrofit2.Retrofit; import retrofit2.converter.gson. questions = response.body(); ArrayAdapter<Question> arrayAdapter = new makeText( MainActivity.this, "Upvote successful", Toast. You can integrate the converter by adding it as a dependency to your app's build.gradle: dependencies { // Retrofit compile 'com.squareup.retrofit2:retrofit:2.5.0' // Retrofit Scalars Converter compile 'com.squareup.retrofit2:converter-scalars:2.5.0' } Next, you need to describe the endpoint you want to interact with.

Retrofit2, The first thing we need to do is add the Retrofit and GSON Converter is the endpoint that our GET request should hit (we'll set the base url a little later). getDevices(); callArray.enqueue(new Callback<Response<List<Device>>() @ Override public void onFailure(Throwable t) { // Go ahead and handle the error here } });� Firstly we declare a RecyclerView in our XML file and then get the reference of it in our Activity. After that we create POJO Class, API Interface, Retrofit builder and then implement the API in our onCreate method of MainActivity. After getting the success response from server we are calling our adapter to set the data in RecyclerView. Download Code

Check the type and annotations to see if the Factory can create the appropriate converter. For example, maybe only Foo types are wrapped, so return null for non-Foo types. Or, if some Foo types are wrapped, and some are not, create an annotation for the ones that are and check in the Factory similarly.

Comments
  • Error is sent in errorBody. I want Retrofit to convert it automatically, not by myself everytime because there are lots of API call methods in my App.
  • see my edit, here if you get error than errorMessage will not null otherwise it will null Suggestion : if you are getting same response for error in all api than you can make it as BaseModel and extends it in all model for all apis
  • In case of error I do not get error response directly. It comes in Response class's errorBody
  • i'm not getting you