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 ...
随机推荐
- 用DIV+Css+Jquery 实现的旧版微信飞机大战。
用jquery 实现的旧版微信飞机大战. 以前一直都是做后台和业务逻辑,前端很少去做, 现在个小游戏. 方向键控制方向,Ctrl 键 放炸弹(当然你的有炸弹,哈哈)! 主要都是用div+Css实现的, ...
- HDU_2055——刷题不要使用fflush()
Problem Description we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26; G ...
- fzu1759:数论高次幂降幂
题目大意: 求 a^b mod c的值..但是b会非常大(10^1000000) 所以需要用到一个数论公式: A^x = A^(x % Phi(C) + Phi(C)) (mod C) 证明见ac大神 ...
- Remove Node in Binary Search Tree 解答
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...
- css流式和弹性布局(未完)
(子容器%父容器)*100 png透明ie6中filter:progid:DXImageTransfornm.Microsoft.AlphaImageLoader( src='路径',sizingMe ...
- (转)OpenSSL命令---pkcs12
用途: pkcs12文件工具,能生成和分析pkcs12文件.PKCS#12文件可以被用于多个项目,例如包含Netscape. MSIE 和 MS Outlook. 用法: openssl pkcs12 ...
- 【小程序开发】微信小程序开发中遇到的那些坑...
第一坑: 设置了三个tabBar,却默认显示第二个,不能展示我的第一个[首页]. "list": [{ "pagePath":"page/KTGJ/i ...
- hibernate jpa 注解 @Temporal(TemporalType.DATE) 格式化时间日期,页面直接得到格式化类型的值
1.日期: @Temporal(TemporalType.DATE) @Column(name = "applyDate", nullable = false, length = ...
- uva 10626 - Buying Coke(记忆化搜索)
题目链接:10626 - Buying Coke 题目大意:给出要买可乐的数量, 以及1元,5元和10元硬币的数量, 每瓶可乐8元,每次照钱会按照最少硬币的方式找回, 问如何投币可使得投入的硬币数最少 ...
- POJ 2318 TOYS/POJ 2398 Toy Storage
计算几何终于开坑了... 叉积+二分. #include<iostream> #include<cstdio> #include<cstring> #include ...