2

I have android app . in that app I'm posting some string data on server and get some response. Problem is ,I'm receiving the response in jsonstring,but I want this data in json array. althouugh when I'm using JsonArrayRequest ,it didn't allow post method in parameter and then my web service is not worked. So I'm stick with StringRequest and service works ok but complete response returns as a whole string.So I'm unable to display my data my list view . So how to resolve this issue?

Here is my code:

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(NewSearchPeople.this, response, Toast.LENGTH_LONG).show();

//                        search1();
                        try {
                            JSONArray jsonArray = new JSONArray(response);

                            for(int i=0;i<jsonArray.length();i++){
                                JSONObject obj = jsonArray.getJSONObject(i);

                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("fullname"));
                                movie.setThumbnailUrl(obj.getString("image"));
                                movie.setRating(obj.getString("location"));
                                movie.setGenre(obj.getString("Description"));

                                movie.setYear(obj.getInt("id"));

                                  movieList.add(movie);
                            }

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


                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(NewSearchPeople.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("fullname", "pooja");

                return params;
            }

        };

Here is json response:

[
{
    "id":"442",
    "fullname":"xyz(18 yr)",
    "image":"\/2017-02-0823:49:521486619389674.jpg",
    "location":"lkn","Description":null
},
{
    "id":"443",
    "fullname":"abc(28 yr)",
    "image":"\/2017-02-0823:51:381486619493934.jpg",
    "location":"md","Description":null
},
{
    "id":"444",
    "fullname":"Arya(25 yr)",
    "image":"\/2017-02-0823:52:251486619540695.jpg",
    "location":"ud","Description":null
}
]
Deepak Kumar
  • 1,035
  • 10
  • 18
  • 1
    Possible duplicate of [How to parse nested json array in android using volley library](http://stackoverflow.com/questions/40818219/how-to-parse-nested-json-array-in-android-using-volley-library) – rohitanand Feb 10 '17 at 08:18
  • Please check that `JSONArray` is null or not. than start the parsing. – Farmer Feb 10 '17 at 08:38
  • your code seem fine. please tell me what issue you faced? – Farmer Feb 10 '17 at 08:51

1 Answers1

0

This can be solved in both requests StringReq and JSONArrayReq.

But better is to fix this using JSONArrayRequest and to pass post params you can use getBody() or getParams() method based on type.

JsonArrayRequest stringRequest = new JsonArrayRequest( METHOD_TYPE, url,null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers == null ? super.getHeaders() : headers;
            }

            @Override
            public byte[] getBody(){
                return BODY.toString().getBytes();
            }
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("fullname", "pooja");
                return params;
            }
        };

above is a simple example of how to pass body in post requests.

Sometimes anonymous class structure becomes very messy and unreadable therefore I would suggest create concrete class (may be static inner).

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49