package com.example.volleydemo;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
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.StringRequest;
import com.android.volley.toolbox.Volley;
/**
* Demo描述: 1 利用Volley获取JSON数据 2 利用Volley异步加载图片 1 利用NetworkImageView加载网路图片
*
*/
public class MainActivity extends Activity {
private ImageView mImageView;
private NetworkImageView mNetworkImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mImageView = (ImageView) findViewById(R.id.imageView);
mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);
getJSONByVolley();
getJsonPost();
loadImageByVolley();
showImageByNetworkImageView();
}
/**
* 利用Volley获取JSON数据
*/
private void getJSONByVolley() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String JSONDataUrl = "http://127.0.0.1:8099/api/api.ashx?action=product_list&ver=1&pageindex=1&pageCount=5";
final ProgressDialog progressDialog = ProgressDialog.show(this,
"请稍后...", "正在加载...");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, JSONDataUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//处理返回的JSON数据
Log.e("bbb", response.toString());
if(progressDialog.isShowing() && progressDialog!=null){
progressDialog.dismiss(); }
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// System.out.println("sorry,Error");
Log.e("aaa", arg0.toString());
}
});
requestQueue.add(jsonObjectRequest);
}
/***
* 以POST形式加载数据
*/
private void getJsonPost() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://127.0.0.1:8099/api/api.ashx";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//处理返回的JSON数据
Log.d("Response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
}
}) {
@Override
protected Map<String, String> getParams() {
//POST 参数
Map<String, String> params = new HashMap<String, String>();
params.put("action", "product_list");
params.put("ver", "1");
params.put("pageindex", "1");
params.put("pageCount", "5");
return params;
}
};
requestQueue.add(postRequest);
}
/**
* 利用Volley异步加载图片
*
* 注意方法参数: getImageListener(ImageView view, int defaultImageResId, int
* errorImageResId) 第一个参数:显示图片的ImageView 第二个参数:默认显示的图片资源 第三个参数:加载错误时显示的图片资源
*/
private void loadImageByVolley() {
String imageUrl = "http://d.hiphotos.baidu.com/pic/w%3D310/sign=a00aca825aafa40f3cc6c8dc9b65038c/060828381f30e9246b63e1814c086e061c95f7bf.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);
}
/**
* 利用NetworkImageView显示网络图片
*/
private void showImageByNetworkImageView() {
String imageUrl = "http://d.hiphotos.baidu.com/pic/w%3D310/sign=a00aca825aafa40f3cc6c8dc9b65038c/060828381f30e9246b63e1814c086e061c95f7bf.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);
}
}
<RelativeLayout 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"
>
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
引入Volley非常简单,首先,从git库先克隆一个下来:
git clone https://android.googlesource.com/platform/frameworks/volley
然后生成JAR包 引用
下面是我生成好的JAR
引用volley JAR包 下载地址