Volley简介

在平时的开发过程中,我们的应用几乎总是在和网络打交道, 在android下的网络编程一般都是基于Http协议的 ,常见的是HttpURLConnection和HttpClient 两个类。因为用的比较多,很容易写一些重复的代码,于是就出现了一些比较好的网络框架,像AsyncHttpClient 、Xutils,Universal-Image-Loader(网络图片加载框架)等。

Volley是Google在2013年I/O大会上退出的一款新的网络框架,它集成了AsyncHttpClient和Universal-Image-Loader的优点,在数据量不大但通信频繁的网络操作中,用Volley非常适合。对于下载还是使用Xutils比较好。

Volley jar包下载

鉴于国内上外网的难度,可以上github上下载,https://github.com/mcxiaoke/android-volley一个和官方保持同步的非官方包

在使用过程中,发现它是一个AndroidStudio的包,并没有所谓的jar包。

如何将所谓的AndroidStudio工程导出为jar:详见:http://www.cnblogs.com/BoBoMEe/p/4294267.html

将jar包导入到lib目录下,或者将Volley的/com(此框架中没有设计到UI方面,没有资源文件)文件夹复制到src即可、。

Volley 提供的功能

  • JSON,图像等的异步下载
  • 网络请求的排序(scheduling)
  • 网络请求的优先级处理
  • 缓存处理
  • 多级别取消请求
  • 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)

Volley使用

①.创建RequestQueue对象

②.创建Request对象

③.将Request对象加入RequestQueue

StringRequest

这个类可以用来从服务器获取String,下面是一个简单的GET请求

//创建requestQueue 对象
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
/**
* 创建stringRequest
* String url, 请求地址
* Listener<String> listener,成功的监听
* ErrorListener errorListener 失败的监听
*/
StringRequest stringRequest = new StringRequest("http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
// 添加到requestQueue 对象中
requestQueue.add(stringRequest);
//开始分发
requestQueue.start();

添加网络权限

<uses-permission android:name="android.permission.INTERNET" />

这段代码,就是将百度首页的HTML代码给请求了过来。requestQueue很明显是一个请求队列,里面缓存了所有的HTTP请求。因此我们在一个类中创建一个requestQueue对象即可。

RequestQueue初始化过程

public RequestQueue getRequestQueue() {
// lazy initialize the request queue, the queue instance will be
// created when it is accessed for the first time
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
} return mRequestQueue;
}

在android2.3 之前使用HttpClient,2.3之后使用HttpURLConnection,在这里也有体现

newRequestQueue(Context context, HttpStack stack)中的部分源码:

if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}

Android 网络框架Volley的使用的更多相关文章

  1. Android网络框架Volley(体验篇)

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  2. Android网络框架Volley(实战篇)

      之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...

  3. Android网络框架-Volley实践 使用Volley打造自己定义ListView

    这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...

  4. Android网络框架Volley

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  5. ym—— Android网络框架Volley(终极篇)

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103).谢谢支持! 没看使用过Volley的同学能够,先看看Android网络框架Volley(体验篇)和 ...

  6. ym—— Android网络框架Volley(体验篇)

    VolleyGoogle I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  7. Android 网络框架 volley源码剖析

    转载请注明出处:  http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了, ...

  8. Android 网络框架---Volley

    /** * Volley 可以同时请求多个,允许高并发 * 特性: * 1.JSON.图片等的异步下载 * 2.网络请求的排序(Scheduling) * 3.网络请求的优先级处理 * 4.缓存 * ...

  9. Android热门网络框架Volley详解[申明:来源于网络]

    Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html

随机推荐

  1. linux根目录下各文件的作用

    各文件详列:   /bin 存放常用命令的目录(二进制可执行命令)    /dev 设备特殊文件    /etc 存放配置相关的文件(系统管理和配置文件)    /etc/rc.d 启动的配置文件和脚 ...

  2. ubuntu 实现界面切换

    1.按ALT+CTRL+F1切换到字符界面(Linux实体机) 如果是VMware虚拟机安装的Linux系统,则切换到字符界面的时候需要以下操作 按下ALT+CTRL+SPACE(空格),ALT+CT ...

  3. thinkphp微信开发之jssdk图片上传并下载到本地服务器

    public function test2(){ $Weixin = new \Weixin\Controller\BaseController(); $this->assign('signPa ...

  4. php获取https下的内容

    直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...

  5. webapp开发——‘手机屏幕分辨率’与‘浏览器分辨率’不要混淆

    关于webApp响应式设计遇到的问题,分享给大家,最近在做一个手机webApp,因为我手机是”米3“,屏幕截图大小是1080宽,所以css样式用@media screen and(min-width: ...

  6. yii 验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),

    //验证用户名是否存在                     array("name","unique",'message'=>'用户名已经存在'),

  7. E: Some packages could not be authenticated

    问题:          在Ubuntu上,安装软件时出现了“E: Some packages could not be authenticated”错误. 原因:     表示系统无法验证这个软件包 ...

  8. Windows Phone 8学习 启动器

    1.发邮件 EmailComposeTack email=new EmailComposeTask(); email.To="收件人"; email.Subject="标 ...

  9. [Forward]Use the SharePoint My Tasks Web Part outside of My Sites

    from http://yalla.itgroove.net/2014/04/use-sharepoint-tasks-web-part-outside-sites/ Use the SharePoi ...

  10. Unity3d 基础知识学习 工具篇

    Unity3d  层(layer)的学习 @广州小龙 unity中的层是个很有用的东西, 点击层的下拉框,点击Add Layer,可以打开层管理器,我新建了一个层为test 他的id值是8,这个后面会 ...