0

I am using retrofit2 and here i am retrieving the data object from server which gives the list of objects present inside, how to know the size of the list. the list will provide objects so through those objects i can retrieve clients data in arrays.

{
    "data": [{
        "ClientName": "Shoib hasan",
        "CompanyName": "097676",
        "CityName": "Badin",
        "ContactNo": "sfgt",
        "TotalBalance": "11000.00",
        "AddedOn": "2017-07-18"
    }, {
        "ClientName": "Musakan Chappal",
        "CompanyName": "Muskan Chappal",
        "CityName": "Badin",
        "ContactNo": "",
        "TotalBalance": "15000.00",
        "AddedOn": "2017-07-18"
    }]
}

From Class Data i retrieve data object

Data data = response.body;

now get list size present inside data object

public class Datum {

@SerializedName("ClientName")
@Expose
private String clientName;
@SerializedName("CompanyName")
@Expose
private String companyName;
@SerializedName("CityName")
@Expose
private String cityName;
@SerializedName("ContactNo")
@Expose
private String contactNo;
@SerializedName("TotalBalance")
@Expose
private String totalBalance;
@SerializedName("AddedOn")
@Expose
private String addedOn;

public String getClientName() {
    return clientName;
}

public void setClientName(String clientName) {
    this.clientName = clientName;
}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public String getCityName() {
    return cityName;
}

public void setCityName(String cityName) {
    this.cityName = cityName;
}

public String getContactNo() {
    return contactNo;
}

public void setContactNo(String contactNo) {
    this.contactNo = contactNo;
}

public String getTotalBalance() {
    return totalBalance;
}

public void setTotalBalance(String totalBalance) {
    this.totalBalance = totalBalance;
}

public String getAddedOn() {
    return addedOn;
}

public void setAddedOn(String addedOn) {
    this.addedOn = addedOn;
}

}

this is Example class

public class Example {

@SerializedName("data")
@Expose
private List<Datum> data ;

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

}

this is method in activity

 public void fetch_information(String CityId) {
    this.CityID = CityId;
    ApiInterface = ApiClient.getApiClient().create(Api.class);
    Call<Example> call = ApiInterface.GetClients(CityID);

    call.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {

            data = new Example();
              data.setData(response.body().getData());

             Integer clientlist_size = response.body().getData().size();


            prepareClientsData();
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

        }
    });
}
public void prepareClientsData() {

clients = new String[data.getData().size()];
    for(int i = 0; i<data.getData().size();i++){
        clients[i] = data.getData().get(i).getClientName();

    }
    client_adapter = new ArrayAdapter<String>(EzKhataActivity.this, android.R.layout.simple_list_item_1, clients);

    client_adapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);
    client_spinner.setAdapter(client_adapter);

this is i am getting

image for data object on response body

sushildlh
  • 8,986
  • 4
  • 33
  • 77
arsalan
  • 25
  • 1
  • 9

2 Answers2

0

Use these 2 class as your response class

Datum.class

public class Datum {

@SerializedName("ClientName")
@Expose
private String clientName;
@SerializedName("CompanyName")
@Expose
private String companyName;
@SerializedName("CityName")
@Expose
private String cityName;
@SerializedName("ContactNo")
@Expose
private String contactNo;
@SerializedName("TotalBalance")
@Expose
private String totalBalance;
@SerializedName("AddedOn")
@Expose
private String addedOn;

public String getClientName() {
return clientName;
}

public void setClientName(String clientName) {
this.clientName = clientName;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getCityName() {
return cityName;
}

public void setCityName(String cityName) {
this.cityName = cityName;
}

public String getContactNo() {
return contactNo;
}

public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}

public String getTotalBalance() {
return totalBalance;
}

public void setTotalBalance(String totalBalance) {
this.totalBalance = totalBalance;
}

public String getAddedOn() {
return addedOn;
}

public void setAddedOn(String addedOn) {
this.addedOn = addedOn;
}

}

Result.class

public class Result{

@SerializedName("data")
@Expose
private List<Datum> data = null;

public List<Datum> getData() {
return data;
}

public void setData(List<Datum> data) {
this.data = data;
}

}

Use Result class in your Retrofit Interface as signature.

And use response.body().getData().size() for the number of object in list.

Go through THIS LINK for more explanations.

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • data = new Example(); data.setData(response.body().getData()); Integer clientlist_size = response.body().getData().size(); this gives data Arraylist size 0; – arsalan Jul 19 '18 at 07:24
  • what is example ? – sushildlh Jul 19 '18 at 07:39
  • Example is a class in Retrofit Interface as signature just like you mentioned Result class above i.e data= new Result(); data.setData(response.body().getData()); i am getting response as data object but it is null. – arsalan Jul 19 '18 at 07:45
  • i did post most of it also image will show what i am getting. – arsalan Jul 19 '18 at 08:07
  • just check your url with postman ?? we can't see any problem in code? – sushildlh Jul 19 '18 at 08:08
  • will you share your url after `.com` ?? – sushildlh Jul 19 '18 at 09:36
  • check your cityId while debuging ?? – sushildlh Jul 19 '18 at 09:51
  • 1
    the problem was not the code it was working fine the issue was in Api interface i was passing @POST("ClientsInCity") Call GetClients(@Query"city" String CityId); instead it should be Call GetClients(@Field"city" String CityId); – arsalan Jul 19 '18 at 09:58
0

you can convert the response to jsonobject and get the json array size like this:

JsonObject jsonObject = (JsonObject) response.body();
JsonArray result = jsonObject.getAsJsonArray("data");
int size = result.size();
masoud vali
  • 1,528
  • 2
  • 18
  • 29