引入Volley非常简单,首先,从git库先克隆一个下来:
git clone https://android.googlesource.com/platform/frameworks/volley
然后生成JAR包 引用
下面是我生成好的JAR
引用volley JAR包 下载地址
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">package</span><span style="color: #000000;"> com.example.volleydemo;</span>
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.json.JSONObject;
7
8 import android.app.Activity;
9 import android.app.ProgressDialog;
10 import android.graphics.Bitmap;
11 import android.os.Bundle;
12 import android.support.v4.util.LruCache;
13 import android.util.Log;
14 import android.widget.ImageView;
15
16 import com.android.volley.Request;
17 import com.android.volley.RequestQueue;
18 import com.android.volley.Response;
19 import com.android.volley.VolleyError;
20 import com.android.volley.toolbox.ImageLoader;
21 import com.android.volley.toolbox.ImageLoader.ImageCache;
22 import com.android.volley.toolbox.ImageLoader.ImageListener;
23 import com.android.volley.toolbox.JsonObjectRequest;
24 import com.android.volley.toolbox.NetworkImageView;
25 import com.android.volley.toolbox.StringRequest;
26 import com.android.volley.toolbox.Volley;
27
28 /**
29 * Demo描述: 1 利用Volley获取JSON数据 2 利用Volley异步加载图片 1 利用NetworkImageView加载网路图片
30 *
31 */
32 public class MainActivity extends Activity {
33 private ImageView mImageView;
34 private NetworkImageView mNetworkImageView;
35
36 @Override
37 protected void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.activity_main);
40 init();
41 }
42
43 private void init() {
44 mImageView = (ImageView) findViewById(R.id.imageView);
45 mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);
46 getJSONByVolley();
47 getJsonPost();
48 loadImageByVolley();
49 showImageByNetworkImageView();
50 }
51
52 /**
53 * 利用Volley获取JSON数据
54 */
55 private void getJSONByVolley() {
56 RequestQueue requestQueue = Volley.newRequestQueue(this);
57 String JSONDataUrl = “http://127.0.0.1:8099/api/api.ashx?action=product_list&ver=1&pageindex=1&pageCount=5”;
58 final ProgressDialog progressDialog = ProgressDialog.show(this,
59 “请稍后…”, “正在加载…”);
60
61 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
62 Request.Method.GET, JSONDataUrl, null,
63 new Response.Listener<JSONObject>() {
64 @Override
65 public void onResponse(JSONObject response) {
66 //处理返回的JSON数据
67 Log.e(“bbb”, response.toString());
68 if(progressDialog.isShowing() && progressDialog!=null){
69 progressDialog.dismiss(); }
70 }
71 }, new Response.ErrorListener() {
72 @Override
73 public void onErrorResponse(VolleyError arg0) {
74 // System.out.println(“sorry,Error”);
75 Log.e(“aaa”, arg0.toString());
76 }
77 });
78 requestQueue.add(jsonObjectRequest);
79 }
80
81 /***
82 * 以POST形式加载数据
83 */
84 private void getJsonPost() {
85 RequestQueue requestQueue = Volley.newRequestQueue(this);
86 String url = “http://127.0.0.1:8099/api/api.ashx”;
87 StringRequest postRequest = new StringRequest(Request.Method.POST, url,
88 new Response.Listener<String>() {
89 @Override
90 public void onResponse(String response) {
91 //处理返回的JSON数据
92 Log.d(“Response”, response);
93 }
94 }, new Response.ErrorListener() {
95 @Override
96 public void onErrorResponse(VolleyError error) {
97 // error
98 }
99 }) {
100 @Override
101 protected Map<String, String> getParams() {
102 //POST 参数
103 Map<String, String> params = new HashMap<String, String>();
104 params.put(“action”, “product_list”);
105 params.put(“ver”, “1”);
106 params.put(“pageindex”, “1”);
107 params.put(“pageCount”, “5”);
108 return params;
109 }
110 };
111 requestQueue.add(postRequest);
112 }
113
114 /**
115 * 利用Volley异步加载图片
116 *
117 * 注意方法参数: getImageListener(ImageView view, int defaultImageResId, int
118 * errorImageResId) 第一个参数:显示图片的ImageView 第二个参数:默认显示的图片资源 第三个参数:加载错误时显示的图片资源
119 */
120 private void loadImageByVolley() {
121 String imageUrl = “http://d.hiphotos.baidu.com/pic/w%3D310/sign=a00aca825aafa40f3cc6c8dc9b65038c/060828381f30e9246b63e1814c086e061c95f7bf.jpg”;
122 RequestQueue requestQueue = Volley.newRequestQueue(this);
123 final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
124 20);
125 ImageCache imageCache = new ImageCache() {
126 @Override
127 public void putBitmap(String key, Bitmap value) {
128 lruCache.put(key, value);
129 }
130
131 @Override
132 public Bitmap getBitmap(String key) {
133 return lruCache.get(key);
134 }
135 };
136 ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
137 ImageListener listener = ImageLoader.getImageListener(mImageView,
138 R.drawable.ic_launcher, R.drawable.ic_launcher);
139 imageLoader.get(imageUrl, listener);
140 }
141
142 /**
143 * 利用NetworkImageView显示网络图片
144 */
145 private void showImageByNetworkImageView() {
146 String imageUrl = “http://d.hiphotos.baidu.com/pic/w%3D310/sign=a00aca825aafa40f3cc6c8dc9b65038c/060828381f30e9246b63e1814c086e061c95f7bf.jpg”;
147 RequestQueue requestQueue = Volley.newRequestQueue(this);
148 final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
149 20);
150 ImageCache imageCache = new ImageCache() {
151 @Override
152 public void putBitmap(String key, Bitmap value) {
153 lruCache.put(key, value);
154 }
155
156 @Override
157 public Bitmap getBitmap(String key) {
158 return lruCache.get(key);
159 }
160 };
161 ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
162 mNetworkImageView.setTag(“url”);
163 mNetworkImageView.setImageUrl(imageUrl, imageLoader);
164 }
165
166 }
View Code
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">RelativeLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span>
2 xmlns:tools=”http://schemas.android.com/tools”
3 android:layout_width=”match_parent”
4 android:layout_height=”match_parent”
5 >
6
7 <com.android.volley.toolbox.NetworkImageView
8 android:id=”@+id/networkImageView”
9 android:layout_width=”fill_parent”
10 android:layout_height=”wrap_content”
11 android:layout_centerHorizontal=”true”
12 android:layout_marginTop=”30dip”
13 />
14
15 <ImageView
16 android:id=”@+id/imageView”
17 android:layout_width=”fill_parent”
18 android:layout_height=”wrap_content”
19 android:layout_centerInParent=”true”
20 />
21
22 </RelativeLayout>
View Code
引入Volley非常简单,首先,从git库先克隆一个下来:
git clone https://android.googlesource.com/platform/frameworks/volley
然后生成JAR包 引用
下面是我生成好的JAR
引用volley JAR包 下载地址