0

I have created a ListView in my Android App and I am trying to display my JSON Array in it from my server, using Volley. I know the listview is set up correctly because when I create the volley request like below it displays correctly in the ListView (I have set phoneNoofUser="0123456789" in my PHP so there is no posting of variables from the Android side):

This displays correctly in 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);
    }

But when I post phoneNoofUser from my app (need to do this because in reality it will not always be 0123456789) nothing shows up in my app. However, the JSON array response comes up correctly in Toast. Can you tell me how I can fix the problem?

ListView is not showing each JSON object in ListView, but JSON array response comes up correctly in Toast :

        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);
    }
CHarris
  • 2,693
  • 8
  • 45
  • 71

1 Answers1

0

I solved my problem - displaying my Json objects in a listview - another way. I was already getting a string response in my php file with the stringrequest volley call. So on that response I converted the string to a json array, and then the json array into individual objects, and then displayed those in the ListView.

Here's the code I used :

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


    }
CHarris
  • 2,693
  • 8
  • 45
  • 71