Volley 与 Glide、下拉刷新
网络请求开源库 OKhttp,Volley
1. Volley的简单使用
- 添加依赖 - 1 
 2- //HTTP库 Volley 
 implementation 'com.android.volley:volley:1.1.1'
- 创建请求队列 
- 创建请求 
- 将请求加入到请求队列中 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21- //1. 创建请求队列 
 RequestQueue queue = Volley.newRequestQueue(this);
 //2. 创建请求
 StringRequest request = new StringRequest(
 Request.Method.GET,//请求方法
 url,//请求的URL
 new Response.Listener<String>() {
 
 public void onResponse(String response) {
 textView.setText(response);
 }
 },
 new Response.ErrorListener() {
 
 public void onErrorResponse(VolleyError error) {
 Log.e(TAG, "onErrorResponse: " + error);
 }
 }
 );
 //3. 将请求加入到请求队列中
 queue.add(request);
2. 使用Volley加载图片
- 创建请求队列 
- 创建图片加载器 
- 将URL中的图像加载到ImageView中 - 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- imageView = findViewById(R.id.imageView); 
 //1. 创建请求队列
 RequestQueue queue = Volley.newRequestQueue(this);
 //2. 创建一个图片加载器,第一个参数是请求队列 第二个参数是cache 缓存
 ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
 private LruCache<String, Bitmap> cache = new LruCache<>(50);
 //LRU缓存机制 最近最久使用LRU的设计原理就是,
 // 当数据在最近一段时间经常被访问,那么它在以后也会经常被访问。
 // 这就意味着,如果经常访问的数据,我们需要然其能够快速命中,而不常访问的数据,我们在容量超出限制内,要将其淘汰
 
 public Bitmap getBitmap(String url) {
 return cache.get(url);
 }
 
 public void putBitmap(String url, Bitmap bitmap) {
 cache.put(url, bitmap);
 }
 });
 //3. 将url中的图像添加到ImageView中
 imageLoader.get(url, new ImageLoader.ImageListener() {
 
 public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
 imageView.setImageBitmap(response.getBitmap());
 }
 
 public void onErrorResponse(VolleyError error) {
 Log.e(TAG, "onErrorResponse: " + error);
 }
 });
3. 使用Glide加载图片
- 添加依赖 - 1 
 2
 3- //Glide库 
 implementation 'com.github.bumptech.glide:glide:4.12.0'
 annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
- 代码 
| 1 | Glide.with(this)//context | 
4. SwipeRefreshLayout
- 添加依赖 - 1 
 2- //下拉刷新库 
 implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
- 添加下拉刷新监听 - 1 
 2
 3
 4
 5
 6
 7- //下拉刷新监听 
 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
 
 public void onRefresh() {
 loadImage();
 }
 });
- 在下拉刷新动作结束后,将刷新动作停止 - 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- public void loadImage() { 
 Random random = new Random();
 String url = random.nextBoolean() ? url1 : url2;
 //使用Glide加载图片
 Glide.with(this)//context
 .load(url)//图片URL
 .placeholder(R.drawable.ic_launcher_background)//没有加载出来时的 占位图
 .listener(new RequestListener<Drawable>() {
 
 public boolean onLoadFailed( GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
 if(swipeRefreshLayout.isRefreshing()){
 swipeRefreshLayout.setRefreshing(false);//停止刷新
 }
 return false;
 }
 
 public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
 if(swipeRefreshLayout.isRefreshing()){
 swipeRefreshLayout.setRefreshing(false);//停止刷新
 }
 return false;
 }
 })
 .into(imageView);
 }