排球onResponse方法未触发 [英] Volley onResponse method not triggered
问题描述
我正在尝试解析JSON文件,并从JSON文件中的每个人获取某些信息.我已经创建了一个parseJSON()函数来为我执行此操作,但是我遇到了一个问题.该应用程序可以运行,但不会从JSON文件接收信息.在函数中设置断点后,我意识到该应用程序甚至没有访问onResponse()函数.
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()函数:
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);
}
}
链接到我的json文件: https://api.myjson.com/bins/ldql7
link to my json file: https://api.myjson.com/bins/ldql7
推荐答案
当您需要将JsonArrayRequest
用作JsonObjectRequest
时,您正在执行JsonArrayRequest
. JSON文件的正文包含在一个Json对象中:
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": [...]
}
更改请求类型后,按如下所示修改您的parseJson
方法:
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);
}
这篇关于排球onResponse方法未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!