How to handle Array of objects in response while using StringRequest in android volley

社会主义新天地 提交于 2019-12-12 02:14:24

问题


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
}
]

回答1:


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



来源:https://stackoverflow.com/questions/42154299/how-to-handle-array-of-objects-in-response-while-using-stringrequest-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!