1. 框架特点
(1). 通信更快,更简单
(2). Get、Post网络请求及网络图像的高效率异步处理请求
(3). 排序
(4). 网络请求的缓存
(5). 多级别取消请求
(6). 和Activity生命周期的联动
缺点:不适合上传和下载
优点:高效的Get/Post方式的数据请求交互,网络图片加载和缓存,是谷歌官方推出的框架,性能很稳定和强劲。
2. 网络数据请求
(1). 使用Get方式请求数据
创建Application类:
<span style="white-space:pre"> </span>public class MyApplication extends Application { public static RequestQueue queues; @Override public void onCreate() { super.onCreate(); queues = Volley.newRequestQueue(getApplicationContext()); } public static RequestQueue getHttpQueues() { return queues; } }
使用StringRequest测试GET请求:
<span style="white-space:pre"> </span>private void volleyGetStringReuest() { String url = "http://apis.juhe.cn/mobile/get?phone=13689249616&key=d83212f9ca3a6028fa0d7d77a3ff3bf8"; // 创建请求对象 StringRequest request = new StringRequest(Method.GET, url, new Listener<String>() { // 请求成功时调用 @Override public void onResponse(String arg0) { Toast.makeText(MainActivity.this, arg0, 0).show(); } }, new Response.ErrorListener() { // 请求失败时调用 @Override public void onErrorResponse(VolleyError arg0) { Toast.makeText(MainActivity.this, arg0.toString(), 0) .show(); } }); request.setTag("abcGet"); MyApplication.getHttpQueues().add(request); }
使用JsonObjectRequest测试GET请求:
private void volleyGetJsonObjectReuest() { String url = "http://apis.juhe.cn/mobile/get?phone=13689249616&key=d83212f9ca3a6028fa0d7d77a3ff3bf8"; // 创建请求对象 JsonObjectRequest request = new JsonObjectRequest(Method.GET, url, null, new Listener<JSONObject>() { // 请求成功 @Override public void onResponse(JSONObject arg0) { Toast.makeText(MainActivity.this, arg0.toString(), 0) .show(); } }, new Response.ErrorListener() { // 请求失败 @Override public void onErrorResponse(VolleyError arg0) { Toast.makeText(MainActivity.this, arg0.toString(), 0) .show(); } }); request.setTag("abcGet"); MyApplication.getHttpQueues().add(request); }
(2). 使用Post请求数据
使用StringRequest测试POST请求:
private void volleyStringRequestPost() { String url = "http://apis.juhe.cn/mobile/get?"; StringRequest request = new StringRequest(Method.POST, url, new Listener<String>() { @Override public void onResponse(String arg0) { Toast.makeText(MainActivity.this, arg0, 0).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { Toast.makeText(MainActivity.this, arg0.toString(), 0) .show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("phone", "13002909620"); map.put("key", "d83212f9ca3a6028fa0d7d77a3ff3bf8"); return map; } }; request.setTag("abcGet"); MyApplication.getHttpQueues().add(request); }
3. Volley与Activity联动
(1). Volley与Activity的联动
覆写onStop方法:
@Override protected void onStop() { super.onStop(); MyApplication.getHttpQueues().cancelAll("abcGet"); }
(2). Volley的二次回调封装
首先要创建一个抽象类VolleyInterface:
public abstract class VolleyInterface { public Context mContext; public Listener<String> mListener; public ErrorListener mErrorListener; public abstract void onMySuccess(String result); public abstract void onMyError(VolleyError error); public VolleyInterface(Context context, Listener<String> listener, ErrorListener errorListener) { mContext = context; mListener = listener; mErrorListener = errorListener; } public Listener<String> loadingListener() { mListener = new Listener<String>() { @Override public void onResponse(String arg0) { onMySuccess(arg0); } }; return mListener; } public ErrorListener errorListener() { mErrorListener = new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { onErrorResponse(arg0); } }; return mErrorListener; } }
然后创建一个类,自定义我们的GET和POST请求:
public class VolleyRequest { public static StringRequest stringRequest; public static Context context; /** * 自定义GET * * @param context * @param url * @param tag * @param vif */ public static void requestGet(Context context, String url, String tag, VolleyInterface vif) { MyApplication.getHttpQueues().cancelAll(tag); stringRequest = new StringRequest(Method.GET, url, vif.successListener(), vif.errorListener()); stringRequest.setTag(tag); MyApplication.getHttpQueues().add(stringRequest); MyApplication.getHttpQueues().start(); } /** * 自定义POST * * @param context * @param url * @param tag * @param params * @param vif */ public static void requestPost(Context context, String url, String tag, final Map<String, String> params, VolleyInterface vif) { MyApplication.getHttpQueues().cancelAll(tag); stringRequest = new StringRequest(Method.POST, url, vif.successListener(), vif.errorListener()) { @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } }; stringRequest.setTag(tag); MyApplication.getHttpQueues().add(stringRequest); MyApplication.getHttpQueues().start(); } }
修改测试的代码:
/** * 自定义get方式 */ private void myGet() { String url = "http://apis.juhe.cn/mobile/get?phone=13689249616&key=d83212f9ca3a6028fa0d7d77a3ff3bf8"; VolleyRequest.requestGet(this, url, "abcGet", new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) { @Override public void onMySuccess(String result) { Toast.makeText(MainActivity.this, result, 0).show(); } @Override public void onMyError(VolleyError error) { Toast.makeText(MainActivity.this, error.toString(), 0).show(); } }); }
这样做的好处是:我们可以在VolleyInterface中写好功和失败的两种处理方式,这样在每一个调用者都可以使用。
关于Post我就不做测试了。
4. 获取网络图片
(1). 使用ImageRequest获取网络图片
public class ImageActivity extends Activity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); initView(); String url = "https://www.baidu.com/img/bdlogo.png"; // 宽和高指定0和0就会以原图的方式加载 ImageRequest request = new ImageRequest(url, new Listener<Bitmap>() { @Override public void onResponse(Bitmap arg0) { imageView.setImageBitmap(arg0); } }, 0, 0, Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { imageView.setImageResource(R.drawable.ic_launcher); } }); MyApplication.getHttpQueues().add(request); } private void initView() { imageView = (ImageView) findViewById(R.id.image); } }
(2). 使用ImageLoader和ImageListener缓存网路图片
创建BitmapCache类:
public class BitmapCache implements ImageCache { public LruCache<String, Bitmap> cache; public int max = 10 * 1024 * 1024; public BitmapCache() { cache = new LruCache<String, Bitmap>(max) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; } @Override public Bitmap getBitmap(String arg0) { return cache.get(arg0); } @Override public void putBitmap(String arg0, Bitmap arg1) { cache.put(arg0, arg1); } }
调用测试:
private void testImageLoader(String url) { ImageLoader loader = new ImageLoader(MyApplication.getHttpQueues(), new BitmapCache()); ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher, R.drawable.ic_launcher); loader.get(url, listener, 0, 0); }
(3). 使用ImageLoader和NetworkImageView加载图片:
private void testNetworkImageView(String url) { ImageLoader loader = new ImageLoader(MyApplication.getHttpQueues(), new BitmapCache()); netImageView.setDefaultImageResId(R.drawable.ic_launcher); netImageView.setErrorImageResId(R.drawable.ic_launcher); netImageView.setImageUrl(url, loader); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-11 02:42:39