Volley框架使用(POST)

需要在MyApplication(继承Application)中配置;

public static RequestQueue requestQueue;
    @Override
    public void onCreate() {
        super.onCreate();
        Volley.newRequestQueue(getApplicationContext());
    }
    public static RequestQueue getHttpQueues() {
        return requestQueue;
    }

使用时:

private void login() {
        String url = "POST地址";
        StringRequest request = new StringRequest(Request.Method.POST, url, this,this){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                //添加登陆参数
                return params;
            }
            @Override
            protected com.android.volley.Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    Map<String, String> responseHeaders = response.headers;
                    //打印httpheader
                    L.i("responseHeaders",responseHeaders.toString());
                    String rawCookies = responseHeaders.get("Set-Cookie");
                    //打印cookie
                    L.i("rawCookies",rawCookies);
                    String dataString = new String(response.data, "UTF-8");
                    return com.android.volley.Response.success(dataString, HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return com.android.volley.Response.error(new ParseError(e));
                }
            }
        };
        //加入请求队列
        MyApplication.getHttpQueues().add(request);
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        //请求失败时处理
    }

    @Override
    public void onResponse(String s) {
        //请求成功时处理
    }

 

posted @ 2015-09-13 13:07  JsonLu  阅读(279)  评论(0编辑  收藏  举报