Volley学习文章

郭霖博客文章讲解的很详细

 

Android Volley完全解析(一),初识Volley的基本用法

http://blog.csdn.net/guolin_blog/article/details/17482095

StringRequest的用法

JsonRequest的用法

 

Android Volley完全解析(二),使用Volley加载网络图片

http://blog.csdn.net/guolin_blog/article/details/17482165

ImageRequest的用法

ImageLoader的用法

NetworkImageView的用法

 

Android Volley完全解析(三),定制自己的Request

http://blog.csdn.net/guolin_blog/article/details/17612763

自定义XMLRequest

自定义GsonRequest

 

Android Volley完全解析(四),带你从源码的角度理解Volley

http://blog.csdn.net/guolin_blog/article/details/17656437

 

第四部分比较难懂

 

关于post和get

Android Volley Tutorial – Making HTTP GET, POST, PUT

http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put

Android使用Volley post请求和Get请求

http://www.whatjay.com/?p=525

 

其他文章:

Asynchronous HTTP Requests in Android Using Volley

http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

Volley 源码解析

http://a.codekk.com/detail/Android/grumoon/Volley%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90

网络请求库Volley详解

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0526/2934.html

 

 

 

下面内容来自:http://www.whatjay.com/?p=525

Volley是谷歌官方推出的一个网络操作框架。

下面介绍下Android使用Volley进行Post和GET请求数据的方法和步骤:

private Context mContext;
    private RequestQueue mRequestQueue;
    private StringRequest mStringRequest;

    // 利用Volley实现Post请求
    private void volley_post() {
        String url = "http://aplesson.com/wap/api/user.php?action=login";
        mContext = this;
        mRequestQueue = Volley.newRequestQueue(mContext);
        mStringRequest = new StringRequest(Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("请求结果:" + response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("请求错误:" + error.toString());
                    }
                }) {
            // 携带参数
            @Override
            protected HashMap<String, String> getParams()
                    throws AuthFailureError {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("un", "852041173");
                hashMap.put("pw", "852041173abc");
                return hashMap;
            }

            // Volley请求类提供了一个 getHeaders()的方法,重载这个方法可以自定义HTTP 的头信息。(也可不实现)
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json; charset=UTF-8");
                return headers;
            }

        };

        mRequestQueue.add(mStringRequest);

    }

    private JsonObjectRequest mJsonObjectRequest;

    // 利用Volley实现Json数据请求
    private void volley_json() {
        mContext = this;
        String url = "http://aplesson.com/data/101010100.html";
        // 1 创建RequestQueue对象
        mRequestQueue = Volley.newRequestQueue(mContext);
        // 2 创建JsonObjectRequest对象
        mJsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println("请求结果:" + response.toString());
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("请求错误:" + error.toString());
                    }
                });

        // 3 将JsonObjectRequest添加到RequestQueue
        mRequestQueue.add(mJsonObjectRequest);

    }

    // 利用Volley实现Get请求
    private void volley_get() {
        mContext = this;
        String url = "http://www.aplesson.com/";
        // 1 创建RequestQueue对象
        mRequestQueue = Volley.newRequestQueue(mContext);
        // 2 创建StringRequest对象
        mStringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("请求结果:" + response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("请求错误:" + error.toString());
                    }
                });
        // 3 将StringRequest添加到RequestQueue
        mRequestQueue.add(mStringRequest);
    }

Volley提供了JsonObjectRequest、JsonArrayRequest、StringRequest等Request形式。JsonObjectRequest:返回JSON对象。

JsonArrayRequest:返回JsonArray。

StringRequest:返回String,这样可以自己处理数据,更加灵活。

另外可以继承Request<T>自定义Request。

 

1.3 取消Request

      Activity里面启动了网络请求,而在这个网络请求还没返回结果的时候,Activity被结束了,此时如果继续使用其中的Context等,除了无辜的浪费CPU,电池,网络等资源,有可能还会导致程序crash,所以,我们需要处理这种一场情况。使用Volley的话,我们可以在Activity停止的时候,同时取消所有或部分未完成的网络请求。Volley里所有的请求结果会返回给主进程,如果在主进程里取消了某些请求,则这些请求将不会被返回给主线程。Volley支持多种request取消方式。
1)可以针对某些个request做取消操作:

 

 @Override
    public void onStop() {
        for (Request <?> req : mRequestQueue) {
            req.cancel();
        }
    }

2)取消这个队列里的所有请求:

@Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        mRequestQueue.cancelAll(this);
    }

3)可以根据RequestFilter或者Tag来终止某些请求

 
@Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

        mRequestQueue.cancelAll( new RequestFilter() {});
        or
        mRequestQueue.cancelAll(new Object());
    }

Volley支持http的GET、POST、PUT、DELETE等方法。

 

posted @ 2016-05-15 18:25  月之漫步  阅读(174)  评论(0编辑  收藏  举报