-2

I just started with android development and I am facing a problem that I can't solve.

I call an API in my android app using Volley and I get the following response:

 [
  {
    "id": 25,
    "user_id": 39,
    "positionX": "51.4595484",
    "positionY": "5.4757402",
    "modified": "2016-12-15T00:00:00"
  },
  {
    "id": 26,
    "user_id": 40,
    "positionX": "51.4595518",
    "positionY": "5.4757409",
    "modified": "2016-12-15T00:00:00"
  },
  {
    "id": 27,
    "user_id": 41,
    "positionX": "51.459554",
    "positionY": "5.4757413",
    "modified": "2016-12-15T00:00:00"
  },
  {
    "id": 28,
    "user_id": 42,
    "positionX": "51.459554",
    "positionY": "5.4757413",
    "modified": "2016-12-15T00:00:00"
  }
]

I am trying to fetch data from JSONArray, but I can't make it work... This is my code:

@Override
public void onResponse(String response) {
    try {
        JSONArray jsonArray = new JSONArray(response);
        JSONObject jsonObject = jsonArray.getJSONObject(0);

        for (int i = 0; i < jsonObject.length(); i++) {
            JSONObject row = jsonObject.getJSONObject();
            points.add(new LatLng(row.getDouble("positionX"),row.getDouble("positionY")));
        }
        Toast.makeText(getApplicationContext(), "Updated users info." + points.get(0).toString(), Toast.LENGTH_LONG).show();
    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "error" + e.getMessage(), Toast.LENGTH_LONG).show();
    }
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Barteo
  • 17
  • 3
  • define *but I can't make it work* also *I am facing a problem that I can't solve.* what problem? – Selvin Dec 15 '16 at 12:20
  • 1
    also, you'd probably benefit from using `gson` library – John O'Reilly Dec 15 '16 at 12:23
  • Please, read [this (how to ask)](http://stackoverflow.com/help/how-to-ask) and [this (mcve)](http://stackoverflow.com/help/mcve) before asking, as those will help you get more and better answers from the community. – Bonatti Dec 16 '16 at 11:40
  • @Bonatti thanks! gonna check it out – Barteo Dec 21 '16 at 16:46

2 Answers2

1

you need to simply fetch your row from JSONArray instead of first JSON object so simply do this

1.) Create your JSONAray

2.) Traverse array and fetch JSONObject using index

3.) Fetch the data from JSONObject which was retrieved during 2nd step

    JSONArray jsonArray = new JSONArray(response);

    for (int i = 0; i < jsonArray.length(); i++) {
         JSONObject row = jsonArray.getJSONObject(i);
         points.add(new LatLng(row.getDouble("positionX"),row.getDouble("positionY")));
        }

Improvement : To avoid exceptions on missing keys and value use optDouble which can handle missing values or values in form of string

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Hey, when I try to use your code my application crashes. I am getting the following error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference – Barteo Dec 15 '16 at 12:33
  • @Barteo you simply forgot to initialize your `points` list before using it so it should be `points = new ArrayList();` you can do it inside `oncreate` or make sure to do it before this loop – Pavneet_Singh Dec 15 '16 at 12:35
0

Try out this

@Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject row= jsonArray.getJSONObject(i)                               
                                points.add(new LatLng(row.getDouble("positionX"),row.getDouble("positionY")));
                            }
                            Toast.makeText(getApplicationContext(), "Updated users info." + points.get(0).toString(), Toast.LENGTH_LONG).show();
                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "error" + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }

But I suggest you to use Gson Library to parse json it is best in business

Download -http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm

How to add lib- Android Studio: Add jar as library?

How to use it - https://blog.ajduke.in/2013/07/28/getting-started-with-google-gson/ Or https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples

Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55