问题
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
回答1:
You are doing a JsonArrayRequest when it needs to be a JsonObjectRequest. The body of your JSON file is contained within a Json Object:
{
"results": [...]
}
Once you have changed the request type, modify your parseJson method like so:
private void parseJson() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, "https://api.myjson.com/bins/ldql7", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("RESPONSE", response.toString());
try {
JSONArray array = response.getJSONArray("results");
JSONObject jsonObject = null;
for (int i = 0; i < array.length(); i++) {
jsonObject = array.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("email");
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) {
// TODO: Handle error
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
来源:https://stackoverflow.com/questions/57042159/volley-onresponse-method-not-triggered