【问题标题】:Volley onResponse method not triggeredVolley onResponse 方法未触发
【发布时间】:2019-07-15 14:42:58
【问题描述】:

我正在尝试解析 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

【问题讨论】:

  • 如果 API 实际上以 JSONArray 形式返回响应,则您的语法是正确的,但您必须确认这一点。在 onResponse() 方法的第一条指令上设置断点,然后将鼠标悬停在响应对象上并检查数据类型。正如@MichaelStoddart 所说,它更有可能以 JSONObject 的形式出现,其中包含一个 JSONArray。所以,如果数据类型读取JSONObject,那么他的解决方案是正确的。

标签: android android-volley


【解决方案1】:

当需要JsonObjectRequest 时,您正在执行JsonArrayRequest。 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);

}

【讨论】:

  • 我已经考虑过这个想法。但由于我是 Json 新手,我发现 json 文件是一个用户数组。
  • 下面一行有错误:JSONArray array = response.getJSONArray("results"); JSONArray中的getJSONArray(int)不能应用于(java.lang.String)
  • @VaskoVasilev 是的,JSON 是一个用户数组,但它都包含在单个 JSON 对象中,这就是为什么您需要更改请求类型,否则您将收到异常,因为您不是从请求中获取预期的响应类型。如果你 printStackTrace onErrorResponse 中的错误,你会明白我的意思
  • 我更改了请求的类型和响应。但这导致出现更多错误,主要是在我尝试访问子对象时。
  • 这一行还是有错误:jsonObject = response.getJSONObject(i); JSONObject中的getJSONObject(java.lang.String)不能应用于(int)
猜你喜欢
  • 2018-12-15
  • 2016-06-04
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 2020-01-27
  • 1970-01-01
相关资源
最近更新 更多