android中使用volley库解析json响应的java问题

ohtdti5x  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(269)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

4个月前关门了。
改进这个问题
我正在开发一个食品订购应用程序,因此我很难将json响应以列表视图的形式解析到我的android应用程序中,因此我尝试了以下代码

private void parseJSON(){
        String URL = "http://abcd.xyz.com/api/items"; // also tried URL as "http://abcd.xyz.com/api/items?page=1"

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObject object = new JSONObject(result);//object of JSON array is given here
                            JSONArray jsonArray = object.getJSONArray("data");

                            for (int i=0; i<jsonArray.length(); i++)
                            {
                             JSONObject hit  = jsonArray.getJSONObject(i);
                                String titleViewUrl = hit .getString("name");
                                String imageViewUrl = hit .getString("picture");
                                String descriptionViewUrl = hit .getString("price");
                                //int costViewUrl = hit .getInt("price");

                                mainListOfFish.add(new ListOfFish(imageViewUrl,titleViewUrl,descriptionViewUrl));
                            }
                            mainListOfFishAdapter = new ListOfFishAdapter(MainActivity.this, mainListOfFish);
                            mainRecyclerView.setAdapter(mainListOfFishAdapter);
                            mainListOfFishAdapter.setOnItemClickListener(MainActivity.this);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mainRequestQueue.add(request);
    }

解析json的name、picture和price的响应,其中作为我的json响应由

{
    "success": true,
    "data": {
        "current_page": 1,
        "data": [
            {
                "item_id": "UID-5f4dcdb58985e15989344535f4dcdb589862",
                "name": "Ayra",
                "price": "$10.00",
                "picture": "http://abcd.xyz.com/placeholder.jpg",
                "description": "Ayra",
                "status": "1"
            },
            {
                "item_id": "UID-5f4dcd865301215989344065f4dcd8653016",
                "name": "Fish",
                "price": "$20.00",
                "picture": "http://abcd.xyz.com/placeholder.jpg",
                "description": "Fish",
                "status": "1"
            },
            {
                "item_id": "UID-5f4dcd86711f215989344065f4dcd86711f5",
                "name": "Fish",
                "price": "$20.00",
                "picture": "http://abcd.xyz.com/placeholder.jpg",
                "description": "Fish",
                "status": "1"
            }
        ],
        "first_page_url": "http://abcd.xyz.com/api/items?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://abcd.xyz.com/api/items?page=1",
        "next_page_url": null,
        "path": "http://abcd.xyz.com/api/items",
        "per_page": 10,
        "prev_page_url": null,
        "to": 3,
        "total": 3
    },
    "code": 200
}

虽然在这里,当我正在分析,我没有得到这些回应的名称,图片和价格。。我还尝试了stackoverflow的其他解决方案,因此我无法克服这个问题,期待解决方案。。

mnemlml8

mnemlml81#

必须首先获取名为“data”的jsonobject,然后从中获取jsonarray“data”。

private void parseJSON(){
        String URL = "http://abcd.xyz.com/api/items"; // also tried URL as "http://abcd.xyz.com/api/items?page=1"

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObect o1 = response.getJSONObject("data");
                            JSONArray jsonArray = o1.getJSONArray("data");

                            for (int i=0; i<jsonArray.length(); i++)
                            {
                             JSONObject hit  = jsonArray.getJSONObject(i);
                                String titleViewUrl = hit .getString("name");
                                String imageViewUrl = hit .getString("picture");
                                String descriptionViewUrl = hit .getString("price");
                                //int costViewUrl = hit .getInt("price");

                                mainListOfFish.add(new ListOfFish(imageViewUrl,titleViewUrl,descriptionViewUrl));
                            }
                            mainListOfFishAdapter = new ListOfFishAdapter(MainActivity.this, mainListOfFish);
                            mainRecyclerView.setAdapter(mainListOfFishAdapter);
                            mainListOfFishAdapter.setOnItemClickListener(MainActivity.this);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mainRequestQueue.add(request);
    }

相关问题