Android-Volley框架

引入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);
// 请求对象,注意这里添加了Request.Method.Post参数
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);
}
}){
// 重写getParams方法,以此获得POST参数
@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
//  这样返回的直接就是一个Json对象
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 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 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);
}
}