1、什么是volley

         Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮。Volley名称的由来: a burst or emission of many things or a large amount at once。在Google IO的演讲上,其配图是一幅发射火弓箭的图,有点类似流星。见下图

2、volley能做什么

volley适合小而快的数据传输。Volley应该是简化了网络通信的一些开发,特别是针对以下两种情况:
  • JSON对象
  • 图片加载
 
Volley的便利功能:

Advantages of using Volley:

  1. Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.
  2. Volley provides transparent disk and memory caching.
  3. Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.
  4. Volley provides powerful customization abilities.
  5. Volley provides Debugging and tracing tools

3、volley架构

Volley使用了线程池来作为基础结构,主要分为主线程,cache线程和network线程。主线程和cache线程都只有一个,而NetworkDispatcher线程可以有多个,这样能解决比并行问题。具体可以参考下图,此图节选自Google I/O 演讲。

4、使用volley

4.1 获取volley

引入Volley非常简单,首先,从git库先克隆一个下来:

git clone https://android.googlesource.com/platform/frameworks/volley

然后编译为jar包,再在自己的工程里import进来。如果git下载失败可以在这下载。 VolleyLib

4.2 JsonObjectRequest 简单文本请求

         使用下面的代码实现json数据获取
  mRequestQueue =  Volley.newRequestQueue(this);
String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
pd = ProgressDialog.show(this,"Please Wait...","Please Wait..."); JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG,response.toString());
parseJSON(response);
va.notifyDataSetChanged();
pd.dismiss();
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG,error.getMessage());
}
});
mRequestQueue.add(jr);
 
 

4.3 ImageView

        使用volley异步加载图像
RequestQueue mRequestQueue = Volley.newRequestQueue(this);
final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>(
20);
ImageCache imageCache = new ImageCache() {
@Override
public void putBitmap(String key, Bitmap value) {
mImageCache.put(key, value);
} @Override
public Bitmap getBitmap(String key) {
return mImageCache.get(key);
}
};
ImageLoader mImageLoader = new ImageLoader(mRequestQueue, imageCache);
// imageView是一个ImageView实例
// ImageLoader.getImageListener的第二个参数是默认的图片resource id
// 第三个参数是请求失败时候的资源id,可以指定为0
ImageListener listener = ImageLoader
.getImageListener(imageView, android.R.drawable.ic_menu_rotate,
android.R.drawable.ic_delete);
mImageLoader.get("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg", listener);

4.4 NetworkImageView

NetworkImageView是继承自ImageView,是Volley提供的一个全新的简单加载图片的控件。

NetworkImageView netImgView=(NetworkImageView)findViewById(R.id.volley_img_networkimgeview);
netImgView.setTag("url"); netImgView.setImageUrl("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg",mImageLoader);
/**
* @author 张兴业
*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:@张兴业TBOW
*/
 

参考:

Android网络通信库Volley简介

Google I/O 2013 – Volley: Easy, Fast Networking for Android(ppt)

Google I/O 2013 – Volley: Easy, Fast Networking for Android的更多相关文章

  1. Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!

    Google官方网络框架Volley实战--QQ吉凶测试,南无阿弥陀佛! 这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 无图无真相 直接撸代码了,详细解释都已经写在注释里了 ...

  2. Google官方网络框架-Volley的使用解析Json以及加载网络图片方法

    Google官方网络框架-Volley的使用解析Json以及加载网络图片方法 Volley是什么? Google I/O 大会上,Google 推出 Volley的一个网络框架 Volley适合什么场 ...

  3. 利用百度词典API和Volley网络库开发的android词典应用

     关于百度词典API的说明,地址在这里:百度词典API介绍 关于android网络库Volley的介绍说明,地址在这里:Android网络通信库Volley 首先我们看下大体的界面布局!

  4. 在Visual Studio 2013/2015上使用C#开发Android/IOS安装包和操作步骤

    Xamarin 配置手册和离线包下载 http://pan.baidu.com/s/1eQ3qw8a 具体操作: 安装前提条件 1. 安装Visual Studio 2013,安装过程省略,我这里安装 ...

  5. [转发]在Visual Studio 2010/2012/2013/2015上使用C#开发Android/IOS安装包和操作步骤

    官方学习文档:http://developer.xamarin.com/guides/android/getting_started/ 官方学习例子:http://developer.xamarin. ...

  6. 2013 HTML5中国峰会演讲:Android上的HTML5:过去,现在和将来

    转载请注明原文地址:http://blog.csdn.net/milado_nju ## 会议链接(应用和工具专场) http://2013.html5dw.com/main, 2013年8月10日 ...

  7. Unity上线google商店 用IL2Cpp打包64位版本和Android APP Bundle优化 及产生的bug

    ios刚上线,这边着手改成android版本,我开始使用的是unity2017.4.1版本 上传谷歌商店是出现这两个警告: 要支持64位,但是在2017版本上没有找到64位的打包选项,猜测应该是版本的 ...

  8. volley框架 出现at com.android.volley.Request.<init>

    请求json数据的时候出现这种情况: 原因: // 访问网络,初始化详情 JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET ...

  9. 开发一款高端大气上档次的android应用需要必备的知识——记于2013年末

    Android入门还是很简单的,看两本书,翻阅几篇文章,搭建了开发环境就算入门了.可是怎样开发一款完备的android应用呢,开发一款高端的android应用又需要那些知识呢,作者根据几年的开发经验做 ...

随机推荐

  1. Arm Cache学习总结

    cache,高速缓存,其原始意义是指访问速度比一般随机存取内存(RAM)快的一种RAM,通常它不像系统主存那样使用DRAM技术,而使用昂贵但较快速的SRAM技术. 1.cache映射方式 cache中 ...

  2. php json_decode失败,返回null

    在使用json_decode之前,一定得保证字符串是utf-8编码,而执行json_decode失败的原因有很多,罗列如下: 1)编码不对: 2)字符串格式不对: 3)字符串格式对,但是有异常字符: ...

  3. 【转载】C#根据当前时间获取周,月,季度,年度等时间段的起止时间

    DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek. ...

  4. TypeScript 映射类型

    typescript支持定义类型加入推导式后产生新的类型 属性不变 但会改变对象的使用方式 这个是类型Person中加入ReadOnly推导出的新类型 他的属性全部是只读的 这个是推导出部分属性 这是 ...

  5. jmeter测试http请求使用csv参数

    创建参数化文件 线程组添加CSV Data Set Config配置元件 为请求添加参数 重新运行查看结果 使用随机参数(JMeter有好多函数可以使用) 查看结果发现使用随机生成的数字 3

  6. Android架构须知

    1.了解不同版本号的特性包含IDE的. 如:AsyncTask3.0之后和之前的差别.Android 5.0的新的API.Android 6.0 不能用HttpClient .AS2.0的新特性 等等 ...

  7. Redis 5种数据类型,2种特殊数据处理策略

    5种数据类型 String [html] view plaincopy 1.String   经常使用命令:   除了get.set.incr.decr mget等操作外,Redis还提供了下面一些操 ...

  8. SET IDENTITY_INSERT <Table Name> ON/OFF 转载

    This command is from SQL Server.  This command is to enable the users to set their own value for IDE ...

  9. PHPCMS模块开发相关文件

    名称 类型说明 Guestbook 模块后台控制器 index 模块前台控制器 Install 安装文件夹 ├templates ├config.inc.php ├extention.inc.php ...

  10. python之读取Excel 文件

    # -*- coding: utf-8 -*- """ Created on Thu May 24 13:53:10 2018 @author: Frank " ...