praxmon
praxmon

Reputation: 5121

Retrofit : How to decide the class members?

I've been trying retrofit for Android. The response has been null. If my understanding is correct, this might be because of a 400 response or an incorrect modelling of the response in my model class. The response that I am getting is as follows:

{"itemA":"data",
"itemB":"data",
"itemC":"data",
"ItemC":"",
"result_arr":[{"Val1":"A","Val2":"","id":"id","pr":"$0.00","sid":"a","cid":"a","price":"$0.00","cool_down":"0%","url":"","name":"Name"},
{"Val1":"A","Val2":"","id":"id","pr":"$0.00","sid":"a","cid":"a","price":"$0.00","cool_down":"0%","url":"","name":"Name"}]
,"statusCode":"200"}

The models that I have defined are as follows:

API result

public class APIResultModel {
@SerializedName("itemA")
public String itemA;
@SerializedName("itemB")
public String itemB;
@SerializedName("itemC")
public String itemC;
@SerializedName("itemD")
public string itemD;
@SerializedName("results_arr")
public List<ProductModel> results_arr;
@SerializedName("status_code")
public String statusCode;
}

Result Array Model:

public class ResultArrayModel {
public String val1;
public String val2;
public String id;
public String pr;
public String sid;
public String cid;
public String price;
public String cool_down;
public String url;
public String name;
}

How should a model for this response look like? And how is the model derived from the response values?

Upvotes: 1

Views: 96

Answers (2)

Looking at your code you seem to be using Gson.

In order for Gson to create your pojo, your model's serializedNames have to match the json response you're getting.

You'll have to change:

@SerializedName("status_code")

to:

@SerializedName("statusCode")

Make sure all your attributes are following this rule and you're good to go.

Upvotes: 2

Pedro Rodrigues
Pedro Rodrigues

Reputation: 1688

Given the JSON:

{
    "itemA": "data",
    "itemB": "data",
    "itemC": "data",
    "ItemD": "",
    "result_arr": [
        {
            "Val1": "A",
            "Val2": "",
            "id": "id",
            "pr": "$0.00",
            "sid": "a",
            "cid": "a",
            "price": "$0.00",
            "cool_down": "0%",
            "url": "",
            "name": "Name"
        },
        {
            "Val1": "A",
            "Val2": "",
            "id": "id",
            "pr": "$0.00",
            "sid": "a",
            "cid": "a",
            "price": "$0.00",
            "cool_down": "0%",
            "url": "",
            "name": "Name"
        }
    ],
    "statusCode": "200"
}

Your api result model may be:

public class APIResult {

    public String itemA;

    public String itemB;

    public String itemC;

    public String itemD;

    @SerializedName("results_arr")
    public List<Product> products;

    public String statusCode;
}

And your product model may be:

public class Product {

    @SerializedName("Val1")
    public String val1;

    @SerializedName("Val2")
    public String val2;

    public String id;

    public String pr;

    public String sid;

    public String cid;

    public String price;

    @SerializedName("cool_down")
    public String coolDown;

    public String url;

    public String name;
}

Supposing you are using GSON, you should only use the SerializedName annotation when the field name is not the same as the present in JSON.

There are some applications that do a conversion from JSON to POJO, Tyr for example.

Upvotes: 0

Related Questions