码迷,mamicode.com
首页 > 其他好文 > 详细

Volley框架使用(POST)

时间:2015-09-13 14:36:05      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

需要在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) {
        //请求成功时处理
    }

 

Volley框架使用(POST)

标签:

原文地址:http://www.cnblogs.com/Jsonlu/p/4804616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!