android - 我想通过使用字符串请求从一个 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 中获取此数据
解决方案
用此代码替换
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();
}
推荐阅读
- python - 为每个循环附加到一个新列表
- c - 更新到 Mojave 后,Mac 上的 Netbeans for C 无法正常工作
- oracle - 使用 Spark 从 Oracle 导入数据
- javascript - 样式组件未将样式应用于自定义功能反应组件
- javascript - Dart 有没有类似 GruntJs 的东西?
- swift - 获取播放毫秒数
- java - 如何在spring jdbc模板中制作流式sql?
- acumatica - 一般查询亲子关系
- excel - Excel:将工作表中的条目缩小到出现在另一个工作表上的条目
- c# - 哪些类可以和不能使用 Binaryformatter 序列化