1.什么是Volley

在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient(Apache)等,今年的Google I/O 2013上,Volley发布了。Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。

视频:http://www.youtube.com/watch?v=y(需要翻墙)优酷:http://v.youku.com/v_show/id_XNTU4ODgzNjg4.html

Volley提供的功能
简单来说,它提供了如下的便利功能:

  • 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.
  • Volley provides transparent disk and memory caching.
  • 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.
  • Volley provides powerful customization abilities.
  • Volley provides Debugging and tracing tools
2.使用Volley

从git库先克隆一个下来:git clone https://android.googlesource.com/platform/frameworks/volley

将volley引入工程即可

http请求

      RequestQueue  mQueue = Volley.newRequestQueue(getApplicationContext());
String url = "https://api.instagram.com/v1/media/popular?client_id=e8e3eb77fcab4830947fed8677eeb6cd"; JsonObjectRequest jsonRequet = new JsonObjectRequest(Method.GET, url,
null, new Listener<JSONObject>() {
public void onResponse(JSONObject result) {
Log.i(TAG, "请求成功"+result); }
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "请求失败");
}
});
jsonRequet.setTag(TAG);
mQueue.add(jsonRequet);

ImageView设置图片

RequestQueue  mQueue = Volley.newRequestQueue(getApplicationContext());
imageview = (ImageView) findViewById(R.id.imageview);
mImageLoader = new ImageLoader(mQueue, new ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>( 10 * 1024 * 1024){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
String imageUrl="http://h.hiphotos.baidu.com/pic/w%3D230/sign=cec1af55a044ad342ebf8084e0a30c08/f11f3a292df5e0fec6c784b95d6034a85fdf72ae.jpg";
ImageListener listener = ImageLoader.getImageListener(imageview, android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete);
mImageLoader.get(imageUrl, listener);

使用NetworkImageView

Volley提供了一个新的控件NetworkImageView来代替传统的ImageView,这个控件的图片属性可以通过setImageUrl方法来设置网络图片

 RequestQueue  mQueue = Volley.newRequestQueue(getApplicationContext());
imageview = (NetworkImageView) findViewById(R.id.imageview);
mImageLoader = new ImageLoader(mQueue, new ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>( 10 * 1024 * 1024){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
String imageUrl="http://h.hiphotos.baidu.com/pic/w%3D230/sign=cec1af55a044ad342ebf8084e0a30c08/f11f3a292df5e0fec6c784b95d6034a85fdf72ae.jpg"; imageview.setImageUrl(imageUrl, mImageLoader);

定制自己的Request

https://gist.github.com/ficusk/5474673

3.参考

http://blog.csdn.net/t12x3456/article/details/9221611

http://blog.csdn.net/geekpark/article/details/9380933

http://stackoverflow.com/questions/16626032/volley-post-get-parameters

http://stackoverflow.com/questions/17571759/looking-for-a-documentation-for-the-android-volley-api

(文档)http://files.evancharlton.com/volley-docs/

Volley使用指南的更多相关文章

  1. Volley使用指南第三回(来自developer.android)

    继第二篇之后,再来Volley使用的教程的第三篇,有些翻译我是根据自己的理解,可能有错误的地方,还请多多包涵. 标准请求 这一回课将会告诉你Volley能够完成的3种请求类型 1.StringReqe ...

  2. Volley使用指南第一回(来自developer.android)

    最近闲来想看看android网络方面的东西.google在2013年发布了一个叫做Volley的网络请求框架,我看了一下官网,居然在training里面就有教程.首先,英文的东西看着 还是挺不爽的,特 ...

  3. Volley使用指南第四回(来自developer.android)

    Volley网络请求的第四篇,废话不多说,开始. 这一篇文章将会教你怎样在Volley支持的范围内定制一个请求. 第一步:写一个通用请求: 大多数请求都有已经写好的接口供你调用,如果你的请求是Stri ...

  4. Volley使用指南第二回(来自developer.android)

    上一篇文章翻译了一下google的Volley官方文档,讲到了最基本的发送request.这一次我们来下一回:创建一个自定义RequestQueue. 这篇文章将会教你一步一步创建自己的Request ...

  5. Android最佳实践指南

    Updated on 2016/1/6 修正了一些翻译段落欢迎转载,但请保留译者链接:http://www.jianshu.com/p/613d28a3c8a0 Lessons learned fro ...

  6. RxVolley使用文档 —— RxVolley = Volley + RxJava + OkHttp

    RxVolley使用文档 -- RxVolley = Volley + RxJava + OkHttp 偶然有幸,看到这个框架,便深深的爱上了这个框架,赶紧转载一发到自己的博客上温故而知新,而且作者一 ...

  7. Android RxVolley = Volley + RxJava + OkHttp

    Github:https://github.com/kymjs/RxVolley RxVolley使用文档 V1.0:http://rxvolley.mydoc.io/ 一.RxVolley使用指南 ...

  8. JavaScript权威指南 - 函数

    函数本身就是一段JavaScript代码,定义一次但可能被调用任意次.如果函数挂载在一个对象上,作为对象的一个属性,通常这种函数被称作对象的方法.用于初始化一个新创建的对象的函数被称作构造函数. 相对 ...

  9. UE4新手之编程指南

    虚幻引擎4为程序员提供了两套工具集,可共同使用来加速开发的工作流程. 新的游戏类.Slate和Canvas用户接口元素以及编辑器功能可以使用C++语言来编写,并且在使用Visual Studio 或 ...

随机推荐

  1. windows phone 8.1开发SQlite数据库引用安装

    原文出自:http://www.bcmeng.com/windows-phone-sqlite/ windows phone 8.1开发SQlite数据库引用安装 第一步: 安装SQlite forw ...

  2. ubuntu查看安装的cuda toolkit自带的工具及其他安装文件

    原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5253672.html 1.查看工具 默认目录为:local,进入local:cd /usr/loc ...

  3. V3 微信支付-预支付C#

    首先不得不吐槽下腾讯,升级微信支付为毛不兼容V2版本呢?V2算是白研究了. V3预支付文档几个坑,不知道你们有没有中招 商户号 mch_id 是 String(32) 微信支付分配的商户号   其实是 ...

  4. MapReduce简介以及详细配置

    1.MapReduce(一个分布式运算框架)将数据分为数据块,发送到不同的节点,并行方式处理. 2.NodeManager和DataNode在一个节点上,程序与数据在一个节点. 3.内容分为两个部分 ...

  5. Tcl与Design Compiler (三)——DC综合的流程

    本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 1.基本流程概述 首先 ...

  6. OSPF相关知识与实例配置【第一部分】

    OSPF相关知识与实例配置[基本知识及多区域配置] OSPF(开放式最短路径优先协议)是一个基于链路状态的IGP,相比于RIP有无环路:收敛快:扩展性好等优点,也是现在用的最多的:所以这次实验就针对于 ...

  7. MCMC(三)MCMC采样和M-H采样

    MCMC(一)蒙特卡罗方法 MCMC(二)马尔科夫链 MCMC(三)MCMC采样和M-H采样 MCMC(四)Gibbs采样(待填坑) 在MCMC(二)马尔科夫链中我们讲到给定一个概率平稳分布$\pi$ ...

  8. 【Egret】中tree组件使用案例

    Egret中tree组件使用案例,包含(文本过多时,自动换行功能) 下面代码结合http://bbs.egret.com/forum.php?mod=viewthread&tid=19028& ...

  9. Nginx VS Apache

    作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率 Nginx 静态处理性能比 Apache 高 3倍以上 最核心的区别在于apache是同步多进 ...

  10. JavaScript Array 技巧

    filter():返回该函数会返回true的项组成的数组 ,,,,]; var result = num.filter(function(item,index,array){ ); }) consol ...