我正在解析通过Volley请求库获取的json响应文件,并通过json数组填充List。
public void getAllContacts() {
queue = Volley.newRequestQueue(getApplicationContext());
String tag_json_obj = "json_obj_req";
String url = "http://52.25.169.219:3000/users";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONArray cast = response.getJSONArray("users");
for (int i=0; i<cast.length(); i++)
{
JSONObject actor = cast.getJSONObject(i);
String name = actor.getString("name");
String phoneno = actor.getString("contact");
name1.add(name);
phno1.add(phoneno);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
VolleyLog.d("Tag", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), "Error in fetching contacts", Toast.LENGTH_LONG).show();
}
});
// Add the request to the RequestQueue.
queue.add(jsonObjReq);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading....");
progressDialog.show();
}
但是在获取联系人时获取凌空错误和吐司是错误的。
我的json文件格式是
{"users":"[{\"id\":1,\"name\":\"test_name\",\"contact\":\"23456543\",\"gender\":\"F\",\"age\":234,\"city\":\"delhi\",\"state\":\"india\",\"created_at\":\"2015-07-19T17:58:42.000Z\",\"updated_at\":\"2015-07-19T17:58:42.000Z\",\"district\":\"test_district\"},{\"id\":2,\"name\":\"test_name\",\"contact\":\"23456543\",\"gender\":\"F\",\"age\":234,\"city\":\"delhi\",\"state\":\"india\",\"created_at\":\"2015-07-19T17:58:42.000Z\",\"updated_at\":\"2015-07-19T17:58:42.000Z\",\"district\":\"test_district\"}]"}
错误是
08-02 21:20:57.264: E/ViewRootImpl(31835): sendUserActionEvent() mView == null