Home > Back-end >  Why only first Few items are fetched from a JSON Response?
Why only first Few items are fetched from a JSON Response?

Time:09-24

I am trying to parse this URL : http://testproject1.indigierp.com/api/banner_details with POST request. This is the response returned :

{
"msg": true,
"src": [
    {
        "id": 12,
        "banner_name": "https://testproject1.indigierp.com/images/banners/promo1.jpg",
        "user_id": "1",
        "created_at": "2021-09-22 21:46:04",
        "updated_at": "-0001-11-30 00:00:00"
    },
    {
        "id": 14,
        "banner_name": "https://testproject1.indigierp.com/images/banners/promo3.jpg",
        "user_id": "1",
        "created_at": "2021-09-22 21:46:16",
        "updated_at": "-0001-11-30 00:00:00"
    },
    {
        "id": 15,
        "banner_name": "https://testproject1.indigierp.com/images/banners/promo4.jpg",
        "user_id": "1",
        "created_at": "2021-09-22 21:46:28",
        "updated_at": "-0001-11-30 00:00:00"
    },
    {
        "id": 16,
        "banner_name": "https://testproject1.indigierp.com/images/banners/promo4.jpg",
        "user_id": "1",
        "created_at": "2021-09-23 03:30:41",
        "updated_at": "-0001-11-30 00:00:00"
    },
    {
        "id": 17,
        "banner_name": "https://testproject1.indigierp.com/images/banners/discountberry.png",
        "user_id": "1",
        "created_at": "2021-09-23 03:32:37",
        "updated_at": "-0001-11-30 00:00:00"
    }
]

}

Now, on my Android App only the first 2 data (JSON Object ) is shown. This is my Android code :

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, BANNER_URL, null, response -> {

        for (int i = 0; i < response.length(); i  ) {

            try {

                JSONArray jsonArray = response.getJSONArray("src");
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Log.e("OBJ Size", String.valueOf(jsonObject.length()));
                String bannerName = jsonObject.getString("banner_name");
                Log.e("BNAME", bannerName);

                sliderModelList.add(new SliderModel(bannerName));
                sliderAdapter.notifyDataSetChanged();


            } catch (JSONException e) {
                e.printStackTrace();

                Log.e("JSOnException", e.getLocalizedMessage());
            }

I debugged as well. The Log.e OBJ Size returns 5 which is correct , but the next line Log.e("BNAME", bannerName); returns only the first 2 items .

What is wrong here, Please try to help me fix it. Thanks :)

CodePudding user response:

Check with this

for (int i = 0; i < response.length(); i  ) {
        try {
            JSONArray jsonArray = response.getJSONArray("src"); 
             for(int j=0;j<jsonArray.length();j  ){    
            JSONObject jsonObject = jsonArray.getJSONObject(j);
            Log.e("OBJ Size", String.valueOf(jsonObject.length()));
            String bannerName = jsonObject.getString("banner_name");
            Log.e("BNAME", bannerName);
            sliderModelList.add(new SliderModel(bannerName));
            sliderAdapter.notifyDataSetChanged();

}

        } catch (JSONException e) {
            e.printStackTrace();

            Log.e("JSOnException", e.getLocalizedMessage());
        }

CodePudding user response:

response.length() returns "2", because there are 2 fields in the response: "msg" and "src". You're doing the for on a wrong object.

Get the array first, then enumerate it:

JSONArray jsonArray = response.getJSONArray("src");
for (int i = 0; i < jsonArray.length(); i  ) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    ...
  • Related