Retrofit response object with out json array name in android

I am trying to integrate retrofit in one of my existing project. I have converted all the API's into retrofit and stuck in one API. The API is giving JSON array and there is no name for it. I am unable to prepare the response model for my json array.

RESPONSE:

[
    {
        "id": -1,
        "colour": "Other"
    },
    {
        "id": 1,
        "colour": "None"
    },
    {
        "id": 2,
        "colour": "Aquamarine"
    },
    {
        "id": 3,
        "colour": "Arizona Grey"
    },
    {
        "id": 4,
        "colour": "Army Green"
    },
    {
        "id": 5,
        "colour": "Beige Blue"
    },

    {
        "id": 269,
        "colour": "Urban Titanium"
    }
]

I am trying to build response model like this.

    public class ColorResponse{

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("colour")
    @Expose
    private String colour;

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

public String getColour() {
return colour;
}

public void setColour(String colour) {
this.colour = colour;
}

}

Here I am getting only one color from my json array.

Can any one help me to solve this issue.

2 answers

  • answered 2018-07-11 04:27 Vishal G. Gohel

     Call<List<ColorResponse>> getColors()
    

    Make list of ColorResponse class object like above this will work for you.

  • answered 2018-07-11 04:48 Divyanshu Sonwani

    Api Return response in List format so you use following code for getting response

    Call<List<ColorResponse>> getColors()
    

    I hope it's work check it