引入Volley
记得在清单文件中添加网络请求权限
1
| compile 'com.android.volley:volley:1.2.1'
|
get请求
使用Volley发送一条get请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| private void sendGet() {
String url = "https://www.httpbin.org/get";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override public void onResponse(String response) { Log.d(TAG, "onResponse: " + response); } }, new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) { Log.d(TAG, "onErrorResponse: " + error); } });
requestQueue.add(stringRequest); }
|
POST请求
使用Volley发送一条post请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| private void sendPost() { String url = "https://www.httpbin.org/post";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override public void onResponse(String response) { Log.d(TAG, "onResponse: " + response); } }, new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) { Log.d(TAG, "onErrorResponse: " + error); } }){
@Nullable @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<>(); map.put("value1","1111"); return map; } }; requestQueue.add(stringRequest); }
|
JsonRequest
请求Json格式的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| private void sendJson() { String url = "https://www.httpbin.org/get"; RequestQueue requestQueue = Volley.newRequestQueue(this); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { Log.d(TAG, "onResponse: " + response.getString("origin")); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d(TAG, "onErrorResponse: " + error); } }); requestQueue.add(jsonObjectRequest); }
|
加载图片
ImageRequest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private void loadImageView() { String url = "https://www.httpbin.org/image/png"; RequestQueue requestQueue = Volley.newRequestQueue(this); ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override public void onResponse(Bitmap response) { imageView.setImageBitmap(response); } }, 1000, 100, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) { imageView.setImageResource(R.drawable.ic_launcher_background); } }); requestQueue.add(imageRequest); }
|
ImageLoader
ImageLoader更牛逼的地方是它可以指定加载完毕前和加载失败后显示的图片,同时建立图片缓存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private void loadImageView() { String url = "https://www.httpbin.org/image/png"; RequestQueue requestQueue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { @Nullable @Override public Bitmap getBitmap(String url) { return null; }
@Override public void putBitmap(String url, Bitmap bitmap) {
} });
ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_background);
imageLoader.get(url, imageListener, 1000, 1000); }
|
现在是一个空的ImageLoader,如果要实现缓存,需要重写它
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class BitmapCache implements ImageLoader.ImageCache{ private LruCache<String, Bitmap> lruCache;
public BitmapCache() { int maxSize = 10 * 1024 * 1024; lruCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; }
@Nullable @Override public Bitmap getBitmap(String url) { return lruCache.get(url); }
@Override public void putBitmap(String url, Bitmap bitmap) { lruCache.put(url, bitmap); } }
|
ImageLoader这样的话就是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| private void loadImageView() { String url = "https://www.httpbin.org/image/png"; RequestQueue requestQueue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());
ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_background);
imageLoader.get(url, imageListener, 1000, 1000); } public class BitmapCache implements ImageLoader.ImageCache{ private LruCache<String, Bitmap> lruCache;
public BitmapCache() { int maxSize = 10 * 1024 * 1024; lruCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; }
@Nullable @Override public Bitmap getBitmap(String url) { return lruCache.get(url); }
@Override public void putBitmap(String url, Bitmap bitmap) { lruCache.put(url, bitmap); } }
|
NetworkImageView
首先需要在布局文件中添加NetworkImageView
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center" tools:context=".volley.VolleyActivity"> <com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="50dp" android:layout_height="50dp"/> </LinearLayout>
|
加载图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| private void loadImageView() { String url = "https://www.httpbin.org/image/png"; RequestQueue requestQueue = Volley.newRequestQueue(this); ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());
networkImageView.setDefaultImageResId(R.drawable.ic_launcher_foreground);
networkImageView.setErrorImageResId(R.drawable.ic_launcher_background);
networkImageView.setImageUrl(url,imageLoader); }
public class BitmapCache implements ImageLoader.ImageCache{ private LruCache<String, Bitmap> lruCache;
public BitmapCache() { int maxSize = 10 * 1024 * 1024; lruCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; }
@Nullable @Override public Bitmap getBitmap(String url) { return lruCache.get(url); }
@Override public void putBitmap(String url, Bitmap bitmap) { lruCache.put(url, bitmap); } }
|