scott
scott

Reputation: 3202

Retrofit 2 - parse JSON based on response Type

I am using retrofit 2 and i have have api where if user detail available then it will return user detail object or else i will send a message saying user detail not found.

 Call<ResponseBody> getCustomerDetail(@Path(value="userDetailId", encoded=true) String userDetailId);

if i have above call as ResponseBody then if i convert to string then i get string json response.

 Log.d("detail",response.body().string());

My question is how i can convert response to My UserDetail pojo class ? if i add Call<UserDetail> then i cant check if user detail not found.so is there any way to achieve this

I have tried below method but not working

 JSONObject obj = new JSONObject(response.body().string());
                Log.d("userdetail",obj.toString());

another method

  Gson gson = new GsonBuilder().create();
              UserDetail userDetail=gson.fromJson(obj.toString(),UserDetail.class );

Three scenarios of response

{
"customer_id": 138,
"customer_name": "John",
"customer_address": "Usa",
"customer_primary_mobile": "2353253232325",
"customer_secondary_mobile": "325322353232",
"customer_location": null,
"customer_type_id": 14
}

second

{
"message": "No Records Found"
}

third

{
"message": "Some think went wrong"
}

Upvotes: 0

Views: 1732

Answers (3)

Viktor Stojanov
Viktor Stojanov

Reputation: 708

Update

Create a serializer for the request.

class UserDetailSerializer{
      @SerializedName("customer_id")
      private Integer customerId;

      ...

      @SerializedName("message")
      private String message;
 }

and then you can do some checks and get the UserDetail object or the message. It's important that you use Integer and not int since it may be null.

It's better in my opinion if you separate UserDetailSerializer and UserDetail since the extra message field can introduce some inconsistency in your code.

I find it useful to use the full potential of Retrofit2 and directly get a POJO and not do the convertion from JSON yourself.

Old answer

You can create a POJO as in what you expect the request to contain and then get your UserDetail from it. Since it's a object it can be null and you can see if the user exists or not.

Your object

class MyResponsePOJO{
    @SerializedName("userdetail")
    UserDetail userDetail;

    ...

    public UserDetail getUserDetail(){
         return this.userDetail;
    }
}

and then your Retrofit2 request

MyResponsePOJO getCustomerDetail(@Path(value="userDetailId", encoded=true) String userDetailId);

and simply get it by calling the getter

UserDetail userDetail = myResponsePOJO.getUserDetail();

If you provide the JSON you are getting I can give you a more precises answer.

Upvotes: 0

Navneet Krishna
Navneet Krishna

Reputation: 5017

Since your response type dont change(always give json object) , add all keys within a json object

    public class UserDetail {

@SerializedName("message")
@Expose
private String message;
@SerializedName("customer_id")
@Expose
private Integer customerId;
@SerializedName("customer_name")
@Expose
private String customerName;
@SerializedName("customer_address")
@Expose
private String customerAddress;
@SerializedName("customer_primary_mobile")
@Expose
private String customerPrimaryMobile;
@SerializedName("customer_secondary_mobile")
@Expose
private String customerSecondaryMobile;
@SerializedName("customer_location")
@Expose
private Object customerLocation;
@SerializedName("customer_type_id")
@Expose
private Integer customerTypeId;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Integer getCustomerId() {
return customerId;
}

public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public String getCustomerAddress() {
return customerAddress;
}

public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}

public String getCustomerPrimaryMobile() {
return customerPrimaryMobile;
}

public void setCustomerPrimaryMobile(String customerPrimaryMobile) {
this.customerPrimaryMobile = customerPrimaryMobile;
}

public String getCustomerSecondaryMobile() {
return customerSecondaryMobile;
}

public void setCustomerSecondaryMobile(String customerSecondaryMobile) {
this.customerSecondaryMobile = customerSecondaryMobile;
}

public Object getCustomerLocation() {
return customerLocation;
}

public void setCustomerLocation(Object customerLocation) {
this.customerLocation = customerLocation;
}

public Integer getCustomerTypeId() {
return customerTypeId;
}

public void setCustomerTypeId(Integer customerTypeId) {
this.customerTypeId = customerTypeId;
}

}

then while you get response

UserDetail user= gson.fromJson("add json reponse here", UserDetail.class);

you can access all fields like user.getMessage(), user.getCustomerName()

Upvotes: 2

Vinay Rathod
Vinay Rathod

Reputation: 1332

you can use direct json response to get pojo reference

UserDetail userDetail = new Gson().fromJson(response.body().string(), UserDetail.class);

Upvotes: 0

Related Questions