Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse Array inside Object With Retrofit Android

I am new of Retrofit Library , I used to use Volley I am trying to parse array inside the object but I can't get how to do it here is my Json response

  {
        "response": {
            "code": "1",
            "success": true,
            "customers": [
                {
                    "id": 1,
                    "name": "reem",
                    "customer_type": "1",
                    "address": "45سسسس",
                    "mobile_no": "05684412211",
                    "phone_no": "414511555",
                    "created_at": "2018-07-30 08:26:48",
                    "updated_at": "2018-07-30 08:26:48"
                }
            ]
        }
    }

I want to take the customers array from the response response here is customer model :

public class Customer {

    @SerializedName("id")
    private Integer id;
    @SerializedName("customer_type")
    private Integer customer_type;
    @SerializedName("name")
    private String name;
    @SerializedName("address")
    private String address;
    @SerializedName("mobile_no")
    private String mobile_no;
    @SerializedName("phone_no")
    private String phone_no;

    public Customer(Integer id, Integer customer_type, String name, String address, String mobile_no, String phone_no) {
        this.id = id;
        this.customer_type = customer_type;
        this.name = name;
        this.address = address;
        this.mobile_no = mobile_no;
        this.phone_no = phone_no;
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getCustomer_type() {
        return customer_type;
    }

    public void setCustomer_type(Integer customer_type) {
        this.customer_type = customer_type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile_no() {
        return mobile_no;
    }

    public void setMobile_no(String mobile_no) {
        this.mobile_no = mobile_no;
    }

    public String getPhone_no() {
        return phone_no;
    }

    public void setPhone_no(String phone_no) {
        this.phone_no = phone_no;
    }
}

and here is Data Service interface:

@GET("get_customers")
    Call<List<Customer>> getAllCustomer();

can you please help me to understand how to parse it and thank you .

like image 231
Reem Aziz Avatar asked Oct 11 '25 20:10

Reem Aziz


2 Answers

Make another POJO class which will have List like this

public class Response{

    @SerializedName("response")
    private Response response;

    @SerializedName("code")
    private String code;

    @SerializedName("success")
    private boolean success;

    @SerializedName("customers")
    private List<Customers> customers;

    public void setResponse(Response response){
        this.response = response;
    }

    public Response getResponse(){
        return response;
    }

    public void setCode(String code){
        this.code = code;
    }

    public String getCode(){
        return code;
    }

    public void setSuccess(boolean success){
        this.success = success;
    }

    public boolean isSuccess(){
        return success;
    }

    public void setCustomers(List<Customers> customers){
        this.customers = customers;
    }

    public List<Customers> getCustomers(){
        return customers;
    }
}

Then in your Data Service Interface

@GET("get_customers")
Call<Response> getAllCustomer();

And then you can get the List of customers like this after getting the body from retrofit call

reponse.getCustomers();
like image 68
theanilpaudel Avatar answered Oct 14 '25 10:10

theanilpaudel


the right , api should return only list of customer json

or you should update your respone to

    public class Customer
{
    public int id { get; set; }
    public string name { get; set; }
    public string customer_type { get; set; }
    public string address { get; set; }
    public string mobile_no { get; set; }
    public string phone_no { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
}

public class Response
{
    public string code { get; set; }
    public bool success { get; set; }
    public List<Customer> customers { get; set; }
}

public class RootObject
{
    public Response response { get; set; }
}

    @GET("get_customers")
Call<RootObject> getAllCustomer();
like image 36
Ahmed Barakat Avatar answered Oct 14 '25 09:10

Ahmed Barakat