JSON 使用 volley 的 android Listview 中的响应未正确打印

JSON response not printed properly in android Listview using volley

我有一个 json 响应,我在 listview.It 中打印了 json 中的值,但未打印所有值 correctly.Some json 值印错地方了。

JSON 响应:

```
[
   {
      "HouseNo":"33333333",
      "AreaName":"ghfhgfhg",
      "Landmark":"",

   },
   {

      "HouseNo":"33333333",
      "AreaName":"gfhgfh",
      "Landmark":"",

   }
]

```

这是获取特定值的代码:

  StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();




                            try {
                                JSONArray jsonarray = new JSONArray(response);
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    JSONObject obj = jsonarray.getJSONObject(i);
                                Customer customer = new Customer();
                                customer.setTitle(obj.getString("HouseNo"));
                                customer.setSerial(obj.getString("AreaName"));
                                customer.setService(obj.getString("Landmark"));

                                customerList.add(customer);

                            }

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

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                         adapter.notifyDataSetChanged();
                                  }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        })

部分响应打印正常;但是代替一些响应打印打印其他 response.How 来解决这个问题?

您可以使用 google 的 Gson 库进行此类转换

Customer c = new Gson().fromJson(json,new TypeToken<Customer>(){}.getType());

你的 json 解析代码是 right.Try 以覆盖适配器 class 中的 add() 方法,如果你的适配器扩展到 ArrayAdapter class.

@Override
    public void add(Customer object) {
        customerList.add(object);
        super.add(object);
    }

这将自动更新您的列表视图。

并且如果您使用的是 BaseAdapter.Then,则 模型 class 或用于适配器的列表对象可能存在错误。