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();
}
}