介绍
Volley是一个Http库,能够简化Android的联网操作,但是不适合大型下载或流式传输操作,因为Volley在解析期间会将所有响应保存在内存中
使用
在使用之前,需要添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
添加依赖:
implementation 'com.android.volley:volley:1.1.1'
如果请求对象是StringRequest
GET请求:
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://www.baidu.com";
StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// 请求成功
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 请求失败
}
});
requestQueue.add(stringRequest);
POST请求:
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://www.baidu.com";
StringRequest stringRequest = new StringRequest(StringRequest.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
textView2.setText(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("name", "Tom");
return map;
}
};
requestQueue.add(stringRequest);
基本上和GET请求差不多,只不过需要重写getParams()
方法,设置请求的参数
如果请求对象是JsonObjectRequest
GET请求:
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://wanandroid.com/wxarticle/chapters/json";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(JsonObjectRequest.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
textView2.setText(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
POST请求:
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://www.baidu.com";
Map<String, String> map = new HashMap<>();
map.put("name", "Jack");
JSONObject jsonObject = new JSONObject(map);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(JsonObjectRequest.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
textView2.setText(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
基本和GET请求差不多,只不过POST请求需要传入一个JSONObject
当然,Volley也可以用于加载图片
方式一:使用ImageRequest
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://cdn.pixabay.com/photo/2019/11/26/17/48/girl-4655079__340.jpg";
ImageRequest imageRequest = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
imageView.setImageBitmap(response);
}
},
0, 0, // 代表最大宽高
ImageView.ScaleType.CENTER_INSIDE, // 缩放类型
Bitmap.Config.RGB_565, //颜色设置
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(imageRequest);
方式二:使用ImageLoader
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://cdn.pixabay.com/photo/2019/11/26/17/48/girl-4655079__340.jpg";
ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> lruCache = new LruCache<>(50); // 设置缓存大小
@Override
public Bitmap getBitmap(String url) {
return lruCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
lruCache.put(url, bitmap);
}
});
// 方式一
imageLoader.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
imageView.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
// 方式二
ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView,
R.drawable.xxx, // 默认显示的图片
R.drawable.xxx); // 加载错误显示的图片
imageLoader.get(url, imageListener);
方式三:使用NetworkImageView
NetworkImageView
是Volley自带的自定义控件,继承自ImageView
,需要配合ImageLoader
使用
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://cdn.pixabay.com/photo/2019/11/26/17/48/girl-4655079__340.jpg";
ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> lruCache = new LruCache<>(50); // 设置缓存大小
@Override
public Bitmap getBitmap(String url) {
return lruCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
lruCache.put(url, bitmap);
}
});
networkImageView.setDefaultImageResId(R.drawable.xxx);
networkImageView.setErrorImageResId(R.drawable.xxx);
networkImageView.setImageUrl(url, imageLoader);
关于Volley的使用差不多就这些了
网友评论