Android框架学习笔记04Volley框架

上一篇中我们学习了Retrofit框架,这一篇我们学习另外一个网络请求框架——Volley。Volley框架是Google在2013年I/O大会上推出的一个基于HttpUrlconnection封装的网络通信框架,上一篇我们学习的Retrofit是基于Okhttp封装的。Volley将AsyncHttpClient和Universal-Image-Loader的优点集于了一身,我们只需要很简单的几句代码就可以实现网络通信,我们也不用再关心怎么从网络获取图片、不用关心线程、不用关心图片资源回收等。Volley除了简单易用之外,性能也非常好,对于数据量不大,但是操作很频繁的操作,Volley表现的非常好;但是对于下载文件等大数据量的网络操作,Volley就不是个很好的选择了。

概述

一句话概括Volley框架:Volley是Google推出的一个异步的网络操作和图片加载框架。

Volley网络通信操作是基于队列的,我们只需要把请求添加到请求队列中就可以了,请求队列会一次执行请求,一般情况下,应用的网络操作没有很多很频繁,一个Application对应一个请求队列就可以了;如果是请求网络操作很多很频繁,那么可以一个Activity对应一个请求队列。

Volley框架有以下特征:

  • 自动调度网络请求

  • 多个并发的网络连接

  • 通过使用标准的HTTP缓存机制保持磁盘和内存响应的一致

  • 可扩展性:Volley支持请求优先级、支持取消请求(可以取消一个或者多个请求)

  • 健壮性:Volley是面向接口编程,可定制性强

  • 包含调试和追踪工具

  • 提供简单易用的图片加载

我们看一下Volley的总体设计图

图片来源于网络。从上图中我们可以看出,volley主要是通过两种Diapatch Thread不断从RequestQueue中取出请求,根据是否已缓存调用Cache或Network这两类数据获取接口之一,从内存缓存或是服务器取得请求的数据,然后交由ResponseDelivery去做结果分发及回调处理。

下面我们通过例子使用一下Volley这个框架:

使用

Volley的基本使用步骤是:

  1. 构建一个RequestQueue。

  2. 创建一个Request,Volley提供的Request有:StringRequest、JsonObjectRequest、JsonArrayRequest、ImageRequest,这四种。

  3. 将Request添加到RequestQueue中。

Volley框架的网络操作基本是这几步,下面我们通过实例学习一下Volley的网络操作。

跟其它框架一样,需要在AS的Gradle中添加引用:

compile 'com.mcxiaoke.volley:library:1.0.19'

将上面代码添加到项目的build.gradle中sync就可以了,不过建议先去Volley的GitHub中添加最新的版本,除了添加引用还需要添加网络访问的权限。

Google建议我们使用Volley单例工具类,所以我们写了一个单例工具类

package com.example.frame.common;

import android.content.Context;
import android.graphics.Bitmap;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * Created by :Huawen
 * Created time : 2016/9/26 11:16
 * Description :
 * Github: https://github.com/Devin1102
 */

public class VolleyController {
private static VolleyController instance;
private Context mContext;
private ImageLoader mImageLoader;
private RequestQueue mRequestQueue;

private VolleyController(Context mContext) {
    this.mContext = mContext;
    initData();
}

/**
 * 初始化
 */
private void initData() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(mContext);
    }
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {

            }
        });
    }
}

/**
 * 获取到VolleyController
 *
 * @param mContext
 * @return
 */
public static synchronized VolleyController getInstance(Context mContext) {
    if (instance == null) {
        instance = new VolleyController(mContext);
    }
    return instance;
}

public RequestQueue getRequestQueue() {
    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

}

这里只是一个单例的Volley工具类,提供了过去Volley中RequestQueue和ImageLoader的方法,ImageLoader在我们使用Volley加载图片的时候用到,所以在这里就添加了一个获取ImageLoader的方法。

前面我们说过Volley中提供的Request有四种,分别是:StringRequest、JsonObjectRequest、JsonArrayRequest和ImageRequest,下面我们就分别使用这四个Request实现GET请求和POST请求,ImageRequest用于加载图片。

StringRequest实现GET请求

StringRequest返回的数据是String类型的,StringRequest提供两个构造方法,我们看一下源码:

public class StringRequest extends Request<String> {

private Listener<String> mListener;

/**
 * Creates a new request with the given method.
 *
 * @param method the request {@link Method} to use
 * @param url URL to fetch the string at
 * @param listener Listener to receive the String response
 * @param errorListener Error listener, or null to ignore errors
 */
public StringRequest(int method, String url, Listener<String> listener,
        ErrorListener errorListener) {
    super(method, url, errorListener);
    mListener = listener;
}

/**
 * Creates a new GET request.
 *
 * @param url URL to fetch the string at
 * @param listener Listener to receive the String response
 * @param errorListener Error listener, or null to ignore errors
 */
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
    this(Method.GET, url, listener, errorListener);
}
}

我们可以看到,第一个构造器是多一个参数的,这个参数就是设置请求的方法,第二个构造器没有method这个请求参数,默认就是GET请求。在StringRequest的构造器中,第一个是设置请求方法,第二个是请求的url,第三个是请求成功的回调,第四个是请求失败的回调。下面我们看一下实例:

private void stringReqGet() {
    StringRequest stringReqGet = new StringRequest(Request.Method.GET, UrlUtils.GET_BASE_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i(TAG, "onResponse" + response);
                    Log.i(TAG, "onResponse---stringReqGet---" + getResources().getString(R.string.req_success));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    });
    mRequestQueue.add(stringReqGet);
}

如果是GET请求,那么第一个参数可以不指定,默认是GET请求,我们需要实现两个回调。在一开始的时候我们就说过,Volley是异步的,我们不需要关心线程,请求成功之后更新UI需要通过消息或者其它方式将数据传递过去,不能直接在请求回调中更新UI。需要注意的是:只有将请求添加到RequestQueue中,请求才会执行。

StringRequest实现POST请求

Volley框架试下StringRequest的POST请求非常简单,只需要设置请求方法是POST,然后设置请求参数即可,我们看一下例子源码:

 private void stringReqPost() {
    StringRequest stringReqPost = new StringRequest(Request.Method.POST, UrlUtils.GET_BASE_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i(TAG, "onResponse---stringReqPost---" + getResources().getString(R.string.req_success));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> stringMap = new HashMap<>();
            stringMap.put("username", "Devin");
            stringMap.put("password", "Devin");
            return stringMap;
        }
    };
    mRequestQueue.add(stringReqPost);
}

需要注意的是:请求方法是POST的话,执行请求的时候会调用getParams()方法,所以我们需要在这里设置请求参数,可以使用Map来设置。

JsonObjectRequest实现GET请求

JsonObjectRequest实现GET请求不是很复杂,和StringRequest差不多,我们直接看例子:

private void jsonObjectReqGet() {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(UrlUtils.GET_BASE_URL,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG, "onResponse" + response.toString());
                    Log.i(TAG, "onResponse" + getResources().getString(R.string.req_success));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    });
    mRequestQueue.add(jsonObjectRequest);
}

JsonObjectRequest实现POST请求

private void jsonObjectReqPost() {
    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("username", "Devin");
    hashMap.put("password", "Devin");
    JSONObject jsonObject = new JSONObject(hashMap);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, UrlUtils.GET_BASE_URL,
            jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.i(TAG, "onResponse" + getResources().getString(R.string.req_success));
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };

    mRequestQueue.add(jsonObjectRequest);
}

JsonObjectRequest的POST请求和StringRequest不一样,在JsonObjectRequest中,我们需要封装一个JSONObject,当成参数传递进去,然后还需要调用getHeaders()方法。JsonObjectRequest有5种构造,这里就不贴代码了,非常简单,想要了解的可以自己去看一下源码。

JsonArrayRequest实现GET请求

private void jsonArrayReqGet() {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, UrlUtils.GET_BASE_URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.i(TAG, "onResponse" + response.toString());
                    Log.i(TAG, "onResponse" + getResources().getString(R.string.req_success));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    });
    mRequestQueue.add(jsonArrayRequest);
}

JsonArrayRequest实现POST请求

private void jsonArrayReqPost() {
    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("username", "Devin");
    hashMap.put("password", "Devin");
    JSONArray jsonArray = new JSONArray();
    jsonArray.put(new JSONObject(hashMap));
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, UrlUtils.GET_BASE_URL,
            jsonArray, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.i(TAG, "onResponse" + getResources().getString(R.string.req_success));
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };
    mRequestQueue.add(jsonArrayRequest);
}

JsonArrayRequest的POST请求跟JsonObjectRequest的差不多,具体可以看一下构造器。

使用ImageRequest加载图片

private void loadImage() {
    net_iv.setVisibility(View.INVISIBLE);
    iv_volley.setVisibility(View.VISIBLE);
    ImageRequest imageRequest = new ImageRequest("https://www.baidu.com/img/bd_logo1.png",
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    iv_volley.setImageBitmap(response);
                    Log.i(TAG, "onResponse" + getResources().getString(R.string.req_success));
                }
            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i(TAG, "onErrorResponse" + getResources().getString(R.string.req_failed));
        }
    });
    mRequestQueue.add(imageRequest);
}

从上面的代码中我们可以看到,我们可以直接在成功的回调中更新UI,非常方便。不过使用ImageRequest加载网络图片也逐渐被另外一种方式取代。

使用ImageLoader和NetworkImageView加载图片

使用ImageLoader和NetworkImageView加载图片是Volley框架推荐使用的方法,其中NetworkImageView是Volley框架自带的一个控件,使用非常简单。

private void imageLoader() {
    String imageUrl = "http://i.weather.com.cn/images/cn/public/2016/09/26/26101505A26375E9BD043626FBA69D86BB7687AC.jpg";
    net_iv.setVisibility(View.VISIBLE);
    iv_volley.setVisibility(View.INVISIBLE);
    ImageLoader mImageLoader = VolleyController.getInstance(getContext()).getImageLoader();
    net_iv.setDefaultImageResId(R.mipmap.ic_menu_icon);
    net_iv.setErrorImageResId(R.mipmap.ic_launcher);
    net_iv.setImageUrl(imageUrl, mImageLoader);
}

使用非常简单,直接调用setImageUrl()方法就可以了。

这里只是初步使用Volley框架,还有更高级的用法,比如自定义Request等,欢迎大家留言交流!

上面项目的Demo在我的GitHub上,有需要的可以参考参考,GitHub地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容