使用Android Volley,如何在我的ListView中显示我的JSON数组?

时间:2017-06-01 09:21:52

标签: android json android-volley

我在我的Android应用程序中创建了一个ListView,我正在尝试使用Volley从我的服务器中显示我的JSON数组。我知道listview设置正确,因为当我创建如下所示的凌空请求时,它在ListView中正确显示(我在PHP中设置了phoneNoofUser="0123456789",因此没有从Android端发布变量):

这在ListView中正确显示:

// Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(SelectUserReviews_URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Review review = new Review();
                                review.setCategory(obj.getString("category"));
                                review.setName(obj.getString("name"));
                                review.setPhone(obj.getString("phone"));
                                review.setComment(obj.getString("comment"));

                                // adding movie to movies array
                                reviewList.add(review);

                            } 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();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

但是当我从我的应用发布phoneNoofUser时(需要这样做,因为实际上它并不总是0123456789)我的应用中没有显示任何内容。但是,JSON数组响应在Toast中正确显示。你能告诉我如何解决这个问题吗?

ListView没有在ListView中显示每个JSON对象,但是在Toast中正确显示JSON数组响应:

        StringRequest stringRequest = new StringRequest(Request.Method.POST, SelectUserReviews_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response.toString());
                        //toast comes up with the correct JSON array response
                        //but can't get the JSON array to come up in my ListView
                        Toast.makeText(PopulistoListView.this, response, Toast.LENGTH_LONG).show();

                        JsonArrayRequest movieReq = new JsonArrayRequest(SelectUserReviews_URL,

                                new Response.Listener<JSONArray>() {
                                    @Override
                                    public void onResponse(JSONArray response) {

                                        Log.d(TAG, response.toString());

                                        hidePDialog();

                                        // Parsing json
                                        for (int i = 0; i < response.length(); i++) {
                                            try {


                                                JSONObject obj = response.getJSONObject(i);
                                                Review review = new Review();
                                                review.setCategory(obj.getString("category"));

                                                review.setName(obj.getString("name"));
                                                review.setPhone(obj.getString("phone"));
                                                review.setComment(obj.getString("comment"));

                                                reviewList.add(review);

                                            } 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 getting user: " + error.getMessage());
                                        hidePDialog();

                                    }

                                }) {


                        };

                        // Adding request for getting user info to request queue
                        AppController.getInstance().addToRequestQueue(movieReq);
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error getting user: " + error.getMessage());
                hidePDialog();
            }
        })

        {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_PHONENUMBER_USER, phoneNoofUser);
                return params;

            }

        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

1 个答案:

答案 0 :(得分:0)

我解决了我的问题 - 在列表视图中显示我的Json对象 - 另一种方式。我已经通过stringrequest凌空调用在我的php文件中获得了一个字符串响应。所以在那个响应中,我将字符串转换为json数组,然后将json数组转换为单个对象,然后在ListView中显示它们。

这是我使用的代码:

 StringRequest stringRequest = new StringRequest(Request.Method.POST, SelectUserReviews_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //hide the 'loading' box
                        hidePDialog();

                        Toast.makeText(PopulistoListView.this, response, Toast.LENGTH_LONG).show();

                        for (int i = 0; i < response.length(); i++) {
                        try {
                        JSONArray responseObject = new JSONArray(response);
                            JSONObject obj = responseObject.getJSONObject(i);
                            Review review = new Review();
                            review.setCategory(obj.getString("category"));
                            review.setName(obj.getString("name"));
                            review.setPhone(obj.getString("phone"));
                            review.setComment(obj.getString("comment"));
                           // Toast.makeText(PopulistoListView.this, responseObject.toString(), Toast.LENGTH_LONG).show();

                            reviewList.add(review);

                    }
                        catch (JSONException e) {
                            Log.e("MYAPP", "unexpected JSON exception", e);
                            // Do something to recover ... or kill the app.
                        }
                    }
                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();

                       // System.out.println("size of reviewlist " + reviewList.size());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(PopulistoListView.this, error.toString(), Toast.LENGTH_LONG).show();

                    }

                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_PHONENUMBER_USER, phoneNoofUser);
                return params;

            }

        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }