在Android Volley中使用StringRequest时如何处理对象数组作为响应 [英] How to handle Array of objects in response while using StringRequest in android volley

查看:226
本文介绍了在Android Volley中使用StringRequest时如何处理对象数组作为响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有android app.在该应用程序中,我在服务器上发布了一些字符串数据,并获得了一些响应. 问题是,我正在jsonstring中接收响应,但我希望在json数组中获得此数据. althouugh,当我使用JsonArrayRequest时,它不允许在参数中使用post方法,然后我的Web服务无法正常工作. 所以我坚持使用StringRequest,服务可以正常工作,但是完整的响应作为一个完整的字符串返回,所以我无法显示我的数据 我的列表视图.那么如何解决这个问题呢?

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?

这是我的代码:

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

        };

这是json响应:

[
{
    "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
}
]

推荐答案

这可以在请求StringReqJSONArrayReq中解决.

This can be solved in both requests StringReq and JSONArrayReq.

但是更好的方法是使用JSONArrayRequest修复此问题,并传递post参数,您可以根据类型使用getBody()getParams()方法.

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

这篇关于在Android Volley中使用StringRequest时如何处理对象数组作为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆