我正在尝试将一个JSON对象解析为android中的listview。
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data..");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONArray array = new JSONArray(s);
for (int i=0; i<= array.length(); i++){
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("name"),
o.getString("realname"),
o.getString("team"),
o.getString("firstappearance"),
o.getString("createdby"),
o.getString("publisher"),
o.getString("imageurl"),
o.getString("bio")
);
listItems.add(item);
}
adapter = new MyAdapter(listItems, getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
这是我用来解析使用齐射的JSON的函数。 应用程序正在运行并且没有抛出任何错误,但视图为空,当我使用静态数据而不是JSON时,数据以预期的格式显示。