Android 网络通信框架Volley的简单使用
Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。
Volley提供的功能:
- JSON,图像等的异步下载;
- 网络请求的排序(scheduling)
- 网络请求的优先级处理
- 缓存
- 多级别取消请求
- 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)
如何使用:将原项目打成jar包直接复制到libs就可以使用。关键是如何将源程序打成jar包。
1:首先,从git库先克隆一个下来:
git clone https://android.googlesource.com/platform/frameworks/volley
2:将volley编译成jar包:(我的是Win7+ADT开发环境)
(1)下载 ant (http://ant.apache.org/),下载之后将其解压到随便一盘如 C:\apache-ant-1.9.2
(2)配置环境系统变量:
新建系统变量:

编辑Path:加入 %ANT_HOME%\bin;
(3)测试以下:

表示成功!
3:执行以下命令进行操作:
如果执行 android update project -p . 提示命令不存在,这时需要将 G:\AndroidADT\adt-bundle-windows-x86-20130219\sdk\tools 加入系统变量。

解决方法如下:

在这里我的系统中存在俩个虚拟机,在这里我选择第一个进行操作:

使用ant进行打jar包:


jar 包存在 \bin\目录下面。
==================简单案例
(1)activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="120dip"
android:layout_height="120dip"
android:layout_marginTop="30dip"
/> <ImageView
android:id="@+id/imageView"
android:layout_width="120dip"
android:layout_height="120dip"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TextView
android:id="@+id/tvShowInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
(2)MainActivity.java
package com.example.volleytest; import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap; public class MainActivity extends Activity {
private TextView tvShowInfo=null;
private ImageView mImageView = null;
private NetworkImageView mNetworkImageView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init(); getJSONByVolley();
loadImageByVolley();
showImageByNetworkImageView();
} private void init(){
tvShowInfo = (TextView)findViewById(R.id.tvShowInfo);
mImageView = (ImageView)findViewById(R.id.imageView);
mNetworkImageView = (NetworkImageView)findViewById(R.id.networkImageView);
} private void getJSONByVolley(){
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
final ProgressDialog progressDialog = ProgressDialog.show(this, "This is title", "...Loading..."); requestQueue.add(new JsonObjectRequest(Method.GET,url,null,
new Listener(){
@Override
public void onResponse(Object arg0) {
tvShowInfo.setText(arg0.toString());
progressDialog.dismiss();
}
},null)); requestQueue.start();
} private void showImageByNetworkImageView() {
String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
RequestQueue requestQueue = Volley.newRequestQueue(this); final LruCache<String,Bitmap> lruCache = new LruCache<String,Bitmap>(20);
ImageCache imageCache = new ImageCache(){
@Override
public void putBitmap(String key, Bitmap value) {
lruCache.put(key, value);
} @Override
public Bitmap getBitmap(String key) {
return lruCache.get(key);
}
}; ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
mNetworkImageView.setTag("url");
mNetworkImageView.setImageUrl(imageUrl, imageLoader);
} private void loadImageByVolley() {
String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
RequestQueue requestQueue = Volley.newRequestQueue(this);
final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);
ImageCache imageCache = new ImageCache() {
@Override
public void putBitmap(String key, Bitmap value) {
lruCache.put(key, value);
} @Override
public Bitmap getBitmap(String key) {
return lruCache.get(key);
}
};
ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher);
imageLoader.get(imageUrl, listener); }
}
Android 网络通信框架Volley的简单使用的更多相关文章
- Android 网络通信框架Volley简介(Google IO 2013)
1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient( ...
- [转]Android 网络通信框架Volley简介(Google IO 2013)
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- 【转】Android 网络通信框架Volley简介(Google IO 2013)
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- Android 网络通信框架Volley(一)
转自:http://blog.csdn.net/t12x3456/article/details/9221611 1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫 ...
- Android 网络通信框架Volley简介
1.1. Volley引入的背景在以前,我们可能面临如下很多麻烦的问题. 比如以前从网上下载图片的步骤可能是这样的流程: 在ListAdapter#getView()里开始图像的读取. 通过Async ...
- Android 网络通信框架Volley基本介绍
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- Android 网络通信框架Volley(二)
Volley提供2个静态方法: public static RequestQueue newRequestQueue(Context context) {} public static Request ...
- Android 网络通信框架Volley(三)
NetworkImageView 分析:public class NetworkImageView extends ImageView 他继承自ImageView,并且添加了一个新方法: public ...
- Android网络框架Volley(体验篇)
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
随机推荐
- #pragma execution_character_set的意义
就是设置执行字符集,指示char的执行字符集是UTF-8编码.如果源文件中出现中文,必须要设置为 #if _MSC_VER >= 1600 #pragma execution_ch ...
- js跨越小结
javascript跨域有几种情况: 1.基于同一父域的子域之间,如:a.c.com和b.c.com 2.基于不同的父域之间,如:www.a.com和www.b.com 3.端口的不同,如:www.a ...
- Java---设计模块(工厂方法)
★ 场景和问题 Java程序开发讲究面向接口编程,隐藏具体的实现类,可是如何得到接口呢? 工厂类的命名规范:***Factory 单例工厂方法的命名规范:getInstance() 工厂的本质是&qu ...
- jQuery插件开发 格式与解析3之$.extend()用途
前叙:$.extend()——用途:扩展和继承 1.Object extend() 用一个或多个对象扩展另一个对象,并返回已修改的原始对象.这对于简单继承是一个非常有用的实用工具. (1)扩展:(Do ...
- CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序
http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...
- 杭州电 1372 Knight Moves(全站搜索模板称号)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- java中不常见的keyword:strictfp,transient
1.strictfp, 即 strict float point (精确浮点). strictfp keyword可应用于类.接口或方法.使用 strictfp keyword声明一个方法时,该方法中 ...
- CAsyncSocket
本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 它是一个异步非阻塞Socket封装类,CAsyncSocket::Create()有一个参数指明了你想要处理哪些Socket事 ...
- arclistsg独立单表模型文档列表
arclistsg独立单表模型文档列表 (DedeCMS > 5.3) 名称:arclistsg 功能:类似arclist标签,获取指定单表模型(例如:分类信息),指定栏目,指定排序及呈现样式的 ...
- CentOS7 安装chrome浏览器
本篇文章主要记录如何在CentOS7.0上安装chrome. 1.配置yum下载源: 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo, 并且在该文件中添加 ...