Android的Volley网络Get/Post请求包实例代码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

1. GET请求
    // 1.创建请求队列  
    RequestQueue volleyRequestQueue = Volley.newRequestQueue(this);  
      
    // 2.json get请求处理  
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(  
            Request.Method.GET, "your server url", null,  
            new Response.Listener<JSONObject>() {  
      
                @Override  
                public void onResponse(JSONObject arg0) {  
                    try {  
                        String strState = arg0.getString("state");  
                        if (strState != null  
                                && strState.equalsIgnoreCase("success")) {  
                            System.out.println("request success!");  
                        } else {  
                            System.out.println("request failed!");  
                        }  
                    } catch (Exception e) {  
                        System.out.println("request failed!");  
                    }  
                }  
            }, new Response.ErrorListener() {  
                @Override  
                public void onErrorResponse(VolleyError arg0) {  
                    System.out.println("request failed!");  
                }  
            }) {  
      
        @Override  
        protected Response<JSONObject> parseNetworkResponse(  
                NetworkResponse arg0) {  
            try {  
                JSONObject jsonObject = new JSONObject(new String(  
                        arg0.data, "UTF-8"));  
                return Response.success(jsonObject,  
                        HttpHeaderParser.parseCacheHeaders(arg0));  
            } catch (UnsupportedEncodingException e) {  
                return Response.error(new ParseError(e));  
            } catch (Exception je) {  
                return Response.error(new ParseError(je));  
            }  
        }  
      
    };  
      
    // 3.请求对象放入请求队列  
    volleyRequestQueue.add(jsonObjectRequest);  

2. POST请求

    try {  
        // 1.创建请求队列  
        RequestQueue volleyRequestQueue = Volley.newRequestQueue(this);  
      
        // 2.POST请求参数  
        JSONObject params = new JSONObject();  
        params.put("name", "zhangsan");  
        params.put("password", "123456");  
      
        // 3.json post请求处理  
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(  
                Request.Method.POST, "your server url", params,  
                new Response.Listener<JSONObject>() {  
      
                    @Override  
                    public void onResponse(JSONObject arg0) {  
                        try {  
                            String strState = arg0.getString("state");  
                            if (strState != null  
                                    && strState.equalsIgnoreCase("success")) {  
                                System.out.println("request success!");  
                            } else {  
                                System.out.println("request failed!");  
                            }  
                        } catch (Exception e) {  
                            System.out.println("request failed!");  
                        }  
                    }  
                }, new Response.ErrorListener() {  
                    @Override  
                    public void onErrorResponse(VolleyError arg0) {  
                        System.out.println("request failed!");  
                    }  
                }) {  
      
            @Override  
            protected Response<JSONObject> parseNetworkResponse(  
                    NetworkResponse arg0) {  
                try {  
                    JSONObject jsonObject = new JSONObject(new String(  
                            arg0.data, "UTF-8"));  
                    return Response.success(jsonObject,  
                            HttpHeaderParser.parseCacheHeaders(arg0));  
                } catch (UnsupportedEncodingException e) {  
                    return Response.error(new ParseError(e));  
                } catch (Exception je) {  
                    return Response.error(new ParseError(je));  
                }  
            }  
      
        };  
      
        // 4.请求对象放入请求队列  
        volleyRequestQueue.add(jsonObjectRequest);  
    } catch (Exception e) {  
    }  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: iOS支付宝支付主要代码

下一篇:利用HtmlUnit 模拟浏览器抓取网页数据