本文主要从两个方面对Android Volley框架的使用方法进行讲解,具体内容如下
一、网络请求
1.get方式请求数据
// 1 创建一个请求队列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); // 2 创建一个请求String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api"; StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { // 正确接收数据回调 @Override public void onResponse(String s) { tv_volley_result.setText(s); }}, new Response.ErrorListener() {// 发生异常后的监听回调 @Override public void onErrorResponse(VolleyError volleyError) { tv_volley_result.setText("加载失败" + volleyError); }}); // 3 将创建的请求添加到请求队列中requestQueue.add(stringRequest);
2.post方式请求数据
// 1 创建一个请求队列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); // 2 创建一个post请求String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String s) { tv_volley_result.setText(s); }}, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_volley_result.setText("请求失败" + volleyError); }}) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>();// map.put("value1","param1"); return map; }}; // 3 将post请求添加到队列中requestQueue.add(stringRequest);
3.请求Json数据
// 1 创建一个请求队列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); // 2 创建一个请求String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { tv_volley_result.setText(jsonObject.toString()); }}, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_volley_result.setText("请求失败" + volleyError); }}); // 3 将创建的请求添加到请求队列中requestQueue.add(jsonObjectRequest);
二、图片加载
1.ImageRequest加载图片
// 1 创建一个请求队列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); // 2 创建一个图片的请求String url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap bitmap) { // 正确接收到图片 iv_volley_result.setImageBitmap(bitmap); }}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { iv_volley_result.setImageResource(R.drawable.atguigu_logo); }}); // 3 将请求添加到请求队列中requestQueue.add(imageRequest);
2.Imageloader加载图片
// 创建一个请求队列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache()); // 加载图片String url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";ImageLoader.ImageListener imageListener = imageLoader.getImageListener(iv_volley_result, R.drawable.atguigu_logo, R.drawable.atguigu_logo);imageLoader.get(url, imageListener);
BitmapCache.java
public class BitmapCache implements ImageLoader.ImageCache { private LruCache<String, Bitmap> mCache; public BitmapCache() { int maxSize = 10 * 1024 * 1024;// 10m mCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; } @Override public Bitmap getBitmap(String url) { return mCache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { mCache.put(url, bitmap); }}
3.NetworkImageView加载图片
<com.android.volley.toolbox.NetworkImageView android:id="@+id/iv_volley_networkimagview" android:layout_width="200dp" android:layout_height="200dp" android:visibility="gone"> </com.android.volley.toolbox.NetworkImageView>
// 创建一个请求队列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); // 创建一个ImageloaderImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache()); // 默认图片和异常图片设置iv_volley_networkimagview.setDefaultImageResId(R.drawable.atguigu_logo);iv_volley_networkimagview.setErrorImageResId(R.drawable.atguigu_logo); // 加载图片String url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";iv_volley_networkimagview.setImageUrl(url, imageLoader);
新闻热点
疑难解答