我想通过使用String Request从一个URL获取两个jsonArray

时间:2018-12-10 13:56:46

标签: android

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"
            },
        ]
    }]
}

如何获取此代码帮我如何使用齐射,我想在listview android中获取此数据

如何获取此代码帮我如何使用齐射,我想在listview android中获取此数据

1 个答案:

答案 0 :(得分:0)

替换为该代码

String