Volley 简单使用
来源:互联网 发布:淘宝网贷口子 编辑:程序博客网 时间:2024/06/06 17:33
git仓库:https://github.com/mcxiaoke/android-volley
一、配置
在build.gradle文件中加:compile 'com.mcxiaoke.volley:library:1.0.17'
二、前期准备
1、在AndroidManifest.xml文件中加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
2、重写Application:(详情看代码)
package com.example.yvan.netall;import android.app.Application;import android.text.TextUtils;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.toolbox.ImageLoader;import com.android.volley.toolbox.Volley;/** * Created by Administrator on 2015/12/6. */public class MyApplication extends Application { //默认TAG public static final String TAG = MyApplication.class.getSimpleName(); //请求队列(一般一个app一个请求队列就可以了) private RequestQueue mRequestQueue; //图片加载器(一般一个app一个图片加载器就可以了) private ImageLoader mImageLoader; private static MyApplication appInstance; @Override public void onCreate() { super.onCreate(); appInstance = this; } /** * 单例模式 得到MyApplication的对象 * * @return */ public static synchronized MyApplication getAppInstance() { return appInstance; } /** * 得到请求队列 * 一个app使用一个请求队列就可以了(一般情况下) * * @return */ public RequestQueue getmRequestQueue() { if (mRequestQueue == null) { mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } return mRequestQueue; } /** * 获取ImageLoader(图片加载器) * * @return */ public ImageLoader getmImageLoader() { //保证mRequestQueue不是空的 if (mRequestQueue == null) { mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } //LruBitmapCache()下文有解释 if (mImageLoader == null) { mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache()); } return mImageLoader; } /** * 将请求request加入请求队列,并给该请求设置标志tag * * @param request * @param tag * @param <T> */ public <T> void addToRequestQueue(Request<T> request, String tag) { //给request设置标志tag,如果tag为空,就设置成默认的TAG request.setTag(TextUtils.isEmpty(tag) ? TAG : tag); getmRequestQueue().add(request); } /** * 将请求request加入请求队列,并给该请求设置默认的TAG * * @param request * @param <T> */ public <T> void addToRequestQueue(Request<T> request) { request.setTag(TAG); getmRequestQueue().add(request); } /** * 取消等待中的请求 * * @param tag 根据tag取消对应的请求 */ public void cancelPendingRequest(String tag) { if (mRequestQueue != null) { //取消所有标志为tag的请求 mRequestQueue.cancelAll(tag); } }}
图片缓冲
package com.example.yvan.netall;import android.graphics.Bitmap;import android.util.LruCache;import com.android.volley.toolbox.ImageLoader;/** * 图片缓冲 LruCache * Created by Administrator on 2015/12/6. */public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache { public LruBitmapCache(int maxSize) { super(maxSize); } public LruBitmapCache() { this(getDefaultLruCacheSize()); } /** * 获取默认的图片缓冲大小 * * @return */ public static int getDefaultLruCacheSize() { final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int lruCacheSize = maxMemory / 8; return lruCacheSize; } @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight() / 1024; } /** * 从缓冲中得到图片 * @param url * @return */ @Override public Bitmap getBitmap(String url) { return get(url); } /** * 缓冲图片 * @param url * @param bitmap */ @Override public void putBitmap(String url, Bitmap bitmap) { put(url, bitmap); }}
三、Get和Post方法
无非就是几种Request:StringRequest、JsonObjectRequest、JsonArrayRequest、ImageRequest、自定义Request,选好某个Request后,就可以将该request加入到请求队列中,就算完成了一个网络请求。
1、StringRequest的应用
StringRequest如果要添加请求参数就加上getParams()方法,如果要添加请求头就加上getHeaders()方法;反之,删除。如下:
StringRequest的第一个参数决定请求的方法(Get或Post)
第二参数是URL
第三个参数是请求成功后返回的结果
第四个参数是请求失败后的错误信息
/** * StringRequest的应用 */ StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url, new com.android.volley.Response.Listener<String>() { @Override public void onResponse(String response) { //在UI线程中 Log.e(TAG,"Vollye数据:"+response.toString()); } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //在UI线程中 VolleyLog.d(TAG,"Error:"+error.getMessage()); } }){ //如果是Post或者Get方法有请求参数,可以在此方法中添加,如果没有请求参数,就删除此方法 @Override protected Map<String, String> getParams() throws AuthFailureError { //添加自己的请求参数 Map<String,String> paramsMap = new HashMap<>(); return paramsMap; } //这是添加Http的头文件,如果不需要添加头文件,就删除此方法 @Override public Map<String, String> getHeaders() throws AuthFailureError { //添加自己的头文件 Map<String,String> headersMap = new HashMap<>(); return headersMap; } }; //这个就是调用Application中的方法,添加到请求队列中,完成网络请求,这是在新的线程中运行的MyApplication.getAppInstance().addToRequestQueue(stringRequest);
2、JsonRequest:JsonObjectRequest和JsonArrayRequest是JsonRequest的直接子类
类似StringRequest的用法,只是参数不同
/** * JsonObjectRequest的应用 */ String tag = "json_obj_req"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, "", new com.android.volley.Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { long tid = Thread.currentThread().getId(); Log.e(TAG, "volley线程id:" + tid); Log.e(TAG, "volley返回的数据:" + response.toString()); } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e(TAG, "Error:" + error.getMessage()); } } ); MyApplication.getAppInstance().addToRequestQueue(jsonObjectRequest, tag);
3、ImageRequest:
类似StringRequest,如下:
参数:第三、四个参数代表图片的最大宽、高,如果图片大于该宽、高,就要压缩,如果是0,就表示不做任何处理,图片多大,就显示多大;第五个参数是图片质量。
/** * 下载图片并加载 * @param url * @param imageView */ public static void downloadSetBitmap(String url, final ImageView imageView){ ImageRequest imageRequest = new ImageRequest(url, new com.android.volley.Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { imageView.setImageBitmap(response); } }, 0, 0, Bitmap.Config.RGB_565, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //如果下载出错,设置默认图片 imageView.setImageResource(R.mipmap.ic_launcher); Log.e(TAG,"下载图片出错:"+error.getMessage()); } }); MyApplication.getAppInstance().addToRequestQueue(imageRequest); }
`
- volley的简单使用
- Volley的简单使用
- Volley 简单使用
- Volley的简单使用
- volley的简单使用
- Volley简单的使用
- Volley的简单使用
- Volley的简单使用
- Volley简单使用一
- Volley简单的使用
- Volley的简单使用
- Volley的简单使用
- Volley简单学习使用一
- volley框架的简单使用
- Volley框架的简单使用
- 再捋一捋Volley的简单使用
- Volley简单使用及用法
- Android Volley的简单使用
- 重排n^2宫问题
- C++入门(1)简单变量和数据类型
- mongodb数据库
- Method Swizzling
- 兄弟郊游问题
- Volley 简单使用
- 七种网卡绑定模式详解
- JAVA多态的实现方式
- 【GDKOI2010】载重量问题
- Android中在Button控件上显示倒计时
- SharePreferences自动登录功能
- 分数加减法
- Go学习笔记:关于Java、Python、Go编程思想的不同
- hdoj 5533 Dancing Stars on Me