首页 > 解决方案 > 我想通过使用字符串请求从一个 url 获取两个 jsonArray

问题描述

private void loadCricketPlayer() {

    //getting the progressbar
    final ProgressBar cricketProgressBar = findViewById(R.id.cricketProgressBar);

    //making the progressbar visible
    cricketProgressBar.setVisibility(View.VISIBLE);


    StringRequest stringRequest = new StringRequest(Request.Method.GET, url_new,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    cricketProgressBar.setVisibility(View.INVISIBLE);

                    try {
                        //getting the whole json object from the response
                        JSONObject obj = new JSONObject(response);

                        JSONArray jArray = obj.getJSONArray("squad");

                        for (int i = 0; i < jArray.length(); i++) {

                            JSONObject jsonObject = jArray.getJSONObject(i);


                            JSONArray bArray = obj.getJSONArray("players");
                            for (int j = 0; j < bArray.length(); j++){


                            JSONObject jsonObject1 = bArray.getJSONObject(j);
                            cricket_Player_POJO cricketPlayer = new cricket_Player_POJO(jsonObject1.getString("name"));

                            cricketListItem.add(cricketPlayer);

                            }

                         }

                        cricket_Player_List cricketList = new cricket_Player_List(cricketListItem, getApplicationContext());
                        cricketPLayerlistView.setAdapter(cricketList);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //displaying the error in toast if occurrs
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

这是我的 json 数组数据,用于从服务器获取数据

这是我的 JSONARRAY 示例。对于获取数据:

{
  "squad": [
    {
        "name": "Australia",
        "players": [
            {
                "pid": 7252,
                "name": "Tim Paine"
            },
            {
                "pid": 489889,
                "name": "Pat Cummins"
            },
            {
                "pid": 5334,
                "name": "Aaron Finch"
            },
        ]
    }]
}

我如何获取此代码帮助我我该怎么做我为此使用 volley,我想在 listview android 中获取此数据

我如何获取此代码帮助我我该怎么做我为此使用 volley,我想在 listview android 中获取此数据

标签: android

解决方案


用此代码替换

try {
                    //getting the whole json object from the response
                    JSONObject obj = new JSONObject(response);

                    JSONArray jArray = obj.getJSONArray("squad");

                    for (int i = 0; i < jArray.length(); i++) {

                        JSONObject jsonObject = jArray.getJSONObject(i);


                        JSONArray bArray = jsonObject.getJSONArray("players");
                        for (int j = 0; j < bArray.length(); j++){


                        JSONObject jsonObject1 = bArray.getJSONObject(j);
                        cricket_Player_POJO cricketPlayer = new cricket_Player_POJO(jsonObject1.getString("name"));

                        cricketListItem.add(cricketPlayer);

                        }

                     }

                    cricket_Player_List cricketList = new cricket_Player_List(cricketListItem, getApplicationContext());
                    cricketPLayerlistView.setAdapter(cricketList);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

推荐阅读