返回顶部

收藏

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

更多
  1. GET请求 ```java // 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  
                            &amp;&amp; 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请求 ```java
    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  
                                    &amp;&amp; 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) {  
    }  

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论