zoukankan      html  css  js  c++  java
  • google volley的使用

    1.Volley简介

     Volley是Android平台上的网络通信库,能使网络通信更快、更简单、更健壮。那么在2013年的Google I/O大会上volley发布了。

     Volley名称的由来: a burst or emission of many things or a large amount at once,下面是Google为Volley的配图:

      

    从中可以看出,volley适合通讯频率高,但传输数据量小的情景。

    2.Volley简化了网络通信的一些开发,特别是针对以下两种情况:

      (1).获取JSON对象

      (2).图片的异步加载

    3.Volley架构

      Volley使用了线程池来作为基础结构,主要分为主线程、cache线程、network线程。主线程和cache都只有一个,而network线程可以有多个,这样就能解决并行问题。详细可以参考下图,选自Google I/O 2013演讲

      

    4.使用Volley

      4.1 从git库克隆一个下来

        git clone https://android.googlesource.com/platform/frameworks/volley  

      4.2 从服务端获取json数据

        

    复制代码
     1   //获取json数据
     2     public void getJSONVolley(){
     3         String url = "http://192.168.1.120:8080/VolleyTest?format=json";
     4         //创建请求队列
     5         RequestQueue requestQueue = Volley.newRequestQueue(this);
     6         //创建json请求对象
     7         JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {
     8             @Override
     9             public void onResponse(JSONObject response) {
    10                 Log.i("response","response is:"+response.toString());
    11             }
    12         },new Response.ErrorListener() {
    13             @Override
    14             public void onErrorResponse(VolleyError volleyError) {
    15                 Log.e("error",volleyError.getMessage().toString());//打印异常信息
    16             }
    17         });
    18         requestQueue.add(jsonObjectRequest);
    19     }
    复制代码

      4.3 volley中使用ImageView加载图片

    复制代码
     1 //加载图片
     2     public void loadImageVolley(){
     3         String url = "http://news.xnnews.com.cn/ylxw/201502/W020150203612623574709.jpg";
     4         RequestQueue requestQueue = Volley.newRequestQueue(this);
     5         final LruCache<String,Bitmap> lruCache = new LruCache(20);
     6         ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
     7             @Override
     8             public Bitmap getBitmap(String key) {
     9                 return lruCache.get(key);
    10             }
    11             @Override
    12             public void putBitmap(String key, Bitmap value) {
    13                 lruCache.put(key,value);
    14             }
    15         };
    16         ImageLoader imageLoader = new ImageLoader(requestQueue,imageCache);
    17         ImageLoader.ImageListener imageListener = imageLoader.getImageListener(imageView,R.drawable.ic_launcher,R.drawable.ic_launcher);
    18         imageLoader.get(url,imageListener);
    19     }
    复制代码

      4.4 使用NetworkImageView异步加载图片

    复制代码
     1 //异步加载图片
     2     public void networkImageView() {
     3         String url = "http://news.xnnews.com.cn/ylxw/201502/W020150203612623574709.jpg";
     4         RequestQueue requestQueue = Volley.newRequestQueue(this);
     5         final LruCache<String,Bitmap> lruCache = new LruCache(20);
     6         ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
     7             @Override
     8             public Bitmap getBitmap(String key) {
     9                 return lruCache.get(key);
    10             }
    11             @Override
    12             public void putBitmap(String key, Bitmap value) {
    13                 lruCache.put(key,value);
    14             }
    15         };
    16         ImageLoader imageLoader = new ImageLoader(requestQueue,imageCache);
    17         networkImageView.setTag(url);
    18         networkImageView.setImageUrl(url,imageLoader);
    19     }
    复制代码

    5.总结

     在请求json数据和图片异步加载时,可以考虑使用Volley,有兴趣的可以读下Volley的源码。

    Google I/O 2013 网络框架Volley演讲pdf地址:http://download.csdn.net/detail/t12x3456/5686041

    Google Volley源码下载地址:http://download.csdn.net/detail/u013165880/8452853

  • 相关阅读:
    手机验证码登录注册--
    防止注册机,登录时-验证码图片的生成=前端vue+后端node
    VUE中数据排序sort() / 数据反转 reverse() 的使用
    将后台数据数组对象(对象里边包含数组对象)---改为前端想要的数组对象--改变key值(替换)
    vue--ui:antd pro框架--vue.config.js引入高德地图--描绘轨迹
    创建数据库-表-增加,设置主键
    mysql--各种安装包版本安装
    Vuex异步请求存取步骤
    从后往前查找去掉字符串最指定元素
    python安装第三方库
  • 原文地址:https://www.cnblogs.com/xsyulinzi/p/4297018.html
Copyright © 2011-2022 走看看