0

I have a few test values in my database and i want to fetch them all in android. This is my following JSON output of the values inside my Database:

 [{"email_address":"rainier_gaari@hotmail.com","comment":"qwehgashdgaskdaweq","date_comment":"2014-06-21","time_comment":"08:28:00","password":"rainier1990"},
 {"email_address":"rainier_gaari@hotmail.com","comment":"asfasdasdasd","date_comment":"2104-06-12","time_comment":"09:03:00","password":"rainier1990"}
 {"email_address":"rainier_gaari@hotmail.com","comment":"asdsfafd","date_comment":"2014-06-22","time_comment":"04:44:00","password":"rainier1990"}]

But every time that I run this code: http://prntscr.com/3vhs5j , it only gives me the first line of the JSON output.: http://prntscr.com/3vi34b

How can I show all the rows( instead of 1 row)in android?

Pacheko
  • 133
  • 1
  • 1
  • 13

2 Answers2

0

It would help if you put some of the code for getting the JSON in your question, to make it easier to reference, but, I don't see a loop, which is why it only gets the first line.

You may want to refer to this question: JSON Array iteration in Android/Java but basically use the length property of the JSONArray and loop through that many times.

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
0

You can use a JSONObject provided by the android API. The JSONObject has a method called getJSONArray()

http://developer.android.com/reference/org/json/JSONObject.html#getJSONArray(java.lang.String)

your error there is that you are instantiating a JSONArray with your Object. you should try this:

JSONArray myArray =jsonObject.getJSONArray();

This is a snippet of code from one of my apps that worked very well to retrieve Json data.

 success = jObject.getInt(TAG_SUCCESS);
 vehicles = jObject.getJSONArray(Vehicle.TAG_VEHICLES);

        if(success == 1) {
            // loop through all the vehicles
            for(int i = 0; i < vehicles.length(); i++) {
                JSONObject obj = vehicles.getJSONObject(i);

                // Get each element based on it's tag
                String year = obj.getString(Vehicle.TAG_YEAR);
                String model = obj.getString(Vehicle.TAG_MODEL);
                String brand = obj.getString(Vehicle.TAG_BRAND);
                String color = obj.getString(Vehicle.TAG_COLOR);
                String license_plate = obj.getString(Vehicle.TAG_LICENSE);
                String main_driver = obj.getString(Vehicle.TAG_DRIVER);
                String policeNumber = obj.getString(Vehicle.TAG_POLICENUMBER);
                String driversLicense = obj.getString(Vehicle.TAG_DRIVER_LICENSE);
                String licenseState = obj.getString(Vehicle.TAG_LICENSE_STATE);
                String driverBirthday = obj.getString(Vehicle.TAG_BIRTHDAY_MONTH) + "/" +
                        obj.getString(Vehicle.TAG_BIRTHDAY_DAY) + "/" +
                        obj.get(Vehicle.TAG_BIRTHDAY_YEAR);
                String driverGender = obj.getString(Vehicle.TAG_DRIVER_GENDER);

                ListRowGroup group = new ListRowGroup(brand + " " + year + " " + model, brand);
                group.children.add(brand + " " + year + " " + model);
                group.children.add(policeNumber);
                group.children.add(model);
                group.children.add(color);
                group.children.add(license_plate);
                group.children.add(main_driver);
                group.children.add(driversLicense + "   -   " + licenseState);
                group.children.add(driverBirthday + "   -   " + driverGender);

                vehiclesGroup.append(i, group);
            }
Marcus Gabilheri
  • 1,259
  • 1
  • 14
  • 26