【android】利用volley+Gson POST json请求来接收jsonObject并解析json数据

这里安利一个介绍volley几种常用方法的blog:http://blog.csdn.net/fenghai22/article/details/44061307

进入正题,当request header中content-type = application/json时,说明需要post一个json才能获取response。volly提供了一个方法:

public JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener listener, ErrorListener errorListener) {
        super(method, url, jsonRequest == null?null:jsonRequest.toString(), listener, errorListener);
    }

同时它的父类JsonRequest也说明了它是针对Json的类

    private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", new Object[]{"utf-8"});

上代码

private void transferByVolley(String url){
        RequestQueue requestQueue = Volley.newRequestQueue(mContext);
        Map params = new HashMap();
        params.put("key1", value1);
        params.put("key2", value2);
        params.put("key3",value3);
        JSONObject jsonObject = new JSONObject(params);
	//注意,这里的jsonObject一定要传到下面的构造方法中!下面的getParams()似乎不会传到构造方法中
        final JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST,url, jsonObject,
                new Response.Listener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, "response -> " + response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.getMessage(), error);
            }
        })
        {
	//重写头文件,根据需要设置,本人就是没写头文件,浪费了很多时间
            @Override
            public Map getHeaders() {
                HashMap headers = new HashMap();
                headers.put("Accept", "application/json, text/javascript, */*; q=0.01");
                headers.put("Content-Type", "application/json");

                return headers;
            }
        };
        requestQueue.add(jsonRequest);
    }

至此可以通过response.Listener获取返回的数据
Log.d(TAG, "response -> " + response.toString());

接下来,以返回json数据为例,用Gson进行解析

首先下载Gson jar包

以下是我的json,可以复制到此查看

{"jsonrpc":"2.0","id":"583824503","result":[{"parent_id":false,"child_id":[200000097,200000139,200000109,100006749,200132001,200000161],"id":1002,"name":"Jewelry"},{"parent_id":false,"child_id":[200001648,200118010,200003482,100003141,200000773,200000775,200001092,200000777,200000781,200000782,200000783,200000784,200000785],"id":1001,"name":"Clothing"},{"parent_id":false,"child_id":[],"id":1011,"name":"Home Décor"},{"parent_id":false,"child_id":[],"id":1003001,"name":"Bags"},{"parent_id":false,"child_id":[],"id":1008,"name":"Beauty"},{"parent_id":false,"child_id":[],"id":1006,"name":"Gadgets"},{"parent_id":false,"child_id":[],"id":1010001,"name":"Shoes"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200001648,"name":"Blouses & Shirts"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200118010,"name":"Bottoms"},{"parent_id":[1002,"Jewelry"],"child_id":[],"id":200000097,"name":"Bracelets"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200003482,"name":"Dresses"},{"parent_id":[1002,"Jewelry"],"child_id":[],"id":200000139,"name":"Earrings"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":100003141,"name":"Hoodies"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000773,"name":"Intimates"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000775,"name":"Jackets"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200001092,"name":"Jumpsuits"},{"parent_id":[1002,"Jewelry"],"child_id":[],"id":200000109,"name":"Necklaces"},{"parent_id":[1002,"Jewelry"],"child_id":[],"id":100006749,"name":"Rings"},{"parent_id":[1002,"Jewelry"],"child_id":[],"id":200132001,"name":"Sets"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000777,"name":"Sleep"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000781,"name":"Socks & Hosiery"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000782,"name":"Suits & Sets"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000783,"name":"Sweaters"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000784,"name":"Swimwear"},{"parent_id":[1001,"Clothing"],"child_id":[],"id":200000785,"name":"Tops"},{"parent_id":false,"child_id":[],"id":1005,"name":"Watches"},{"parent_id":[1002,"Jewelry"],"child_id":[],"id":200000161,"name":"Wedding"}]}


用于gson的解析类中需要与json的key值相同,所以第一个新建的类(JsonDate)中需要包含三个参数,分别是 

    private String jasonrpc;
    private long id;
    private List result;
其中result中又包含一个由parent_id、chiled_id、id、name组成的子元素,所以上面的List中的Result类包含四个参数

    private Object parent_id;
    private List child_id;
    private int id;
    private String name;

其中child_id由若干个Long的数组组成,原谅我是新手,之前这里遇到了一个难题就是,parent_id有时候是Boolean有时候是个数组,所以这里使用了object,之后拿数据的时候要记得判断数据类型

最后,都要生成set/get方法

JsonDate status = gson.fromJson(jsonString, JsonDate.class);
生成完JsonDate的对象后就可以从里面通过get方法随便拿数据了。




你可能感兴趣的:(【android】利用volley+Gson POST json请求来接收jsonObject并解析json数据)