排球onResponse方法未触发

时间:2019-07-15 14:42:58

标签: android android-volley

我正在尝试解析JSON文件,并从JSON文件中的每个人获取某些信息。我创建了一个parseJSON()函数来为我执行此操作,但是遇到了一个问题。该应用程序可以运行,但是它没有从JSON文件接收信息。在函数中设置断点后,我意识到该应用程序甚至没有访问onResponse()函数。

我已经阅读了类似的一些类似问题的答案,但它们似乎并没有帮助。 似乎是什么原因造成的?

parseJson()函数:

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

1 个答案:

答案 0 :(得分:1)

您正在做一个JsonArrayRequest的时候JsonObjectRequest。 JSON文件的正文包含在Json对象中:

{
    "results": [...]
}

一旦您更改了请求类型,就可以像这样修改parseJson方法:

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

}