I am trying to parse a JSON file and fetch certain info from every person in the JSON file. I have created a parseJSON() function to do this for me, but I have run into a problem. The application works but it does not receive the information from the JSON file. After placing a breakpoint in the function, I realised that the application does not even acess the onResponse() function.
I have read the answers of some similar questions like this, but they did not seem to be of help. What seems to be the cause of this?
parseJson() function:
private void parseJson() {
JsonArrayRequest request = new JsonArrayRequest(jsonUrl, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
JSONObject nameObject = jsonObject.getJSONObject("name");
String title = nameObject.getString("title");
String firstName = nameObject.getString("first");
String lastName = nameObject.getString("last");
String email = jsonObject.getString("emial");
JSONObject prictureObject = jsonObject.getJSONObject("picture");
String imageUrl = prictureObject.getString("medium");
String fullName = title + " " + firstName + " " + lastName;
Log.e("FULL NAME", fullName);
// Ignore this part
/*Book jsonBook = new Book(imageUrl, fullName, email, 50.0, 100, 3);
Books.add(jsonBook);*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(HomeActivity.this);
}
}
link to my json file: https://api.myjson.com/bins/ldql7
You are doing a
JsonArrayRequest
when it needs to be aJsonObjectRequest
. The body of your JSON file is contained within a Json Object:Once you have changed the request type, modify your
parseJson
method like so: