zoukankan      html  css  js  c++  java
  • Google开源库-Volley的使用

    一、什么是Volley?

    Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.  (上述的意思为:Volley是一个处理Android网络通信的工具,它可以是的Android中的网络通信更加的快速,高效)

                      --->url(https://developer.android.com/training/volley/index.html)

    二、如何在自己的项目中引用Volley?

      image

    ps:上述描述了Volley的使用方式,你需要使用git工具克隆到本地,然后使用eclipse ADT将其转换成jar文件即可使用

    三、Volley的使用讲解

    3.1 使用Valley实现JSON字符串请求

    /**
         * 通过Volley获取JSON数据
         */
        public void getJSONVolley(){
            RequestQueue requestQueue=Volley.newRequestQueue(this); //用于获取一个Volley的请求对象
            String jSONDateUrl="http://www.imooc.com/api/teacher?type=4&num=30";  //请求的Url
            //(Request., url, listener, errorListener)
            //-->请求方式,请求的链接,成功得到请求,错误得到请求
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, jSONDateUrl, null, 
                    //成功得到请求的数据
                    new Response.Listener<JSONObject>() {
                            public void onResponse(JSONObject response) {
                                System.out.println("response= "+response );
                            }
                        },
                        //异常得到请求的数据
                  new Response.ErrorListener() {
                            public void onErrorResponse(com.android.volley.VolleyError arg0) {
                                System.out.println("对不起,有问题");
                            }
                    }
                );
            requestQueue.add(jsonObjectRequest); //在请求队列中加入当前的请求
        }
    

    image

    3.2 使用Volley异步加载图片

     /**
         * 使用Volley异步加载图片
          * url:http://img.mukewang.com//55237dcc0001128c06000338.jpg
         */
        public void loadImageVolley(){
            String imageUrl="http://img.mukewang.com//55237dcc0001128c06000338.jpg"; //图片的链接
            RequestQueue requestQueue=Volley.newRequestQueue(this); //创建Volley的请求对象
            final LruCache<String, Bitmap> lruCache=new LruCache<String,Bitmap>(20); //创建一个缓存对象 缓存大小为20
            ImageCache imageCache=new ImageCache() {
                
                @Override
                public void putBitmap(String key, Bitmap value) {
                        lruCache.put(key, value);
                }
                
                @Override
                public Bitmap getBitmap(String key) {
                    return lruCache.get(key);
                }
            };
            //使用ImageLoad进行图片的加载,加载参数(请求,图片的缓存)
            ImageLoader imageLoader=new ImageLoader(requestQueue, imageCache);
            
            //参数(控件名称,找不到时候的图片,异常时候的图片)
            ImageListener listener=imageLoader.getImageListener(img, R.drawable.ic_launcher, R.drawable.ic_launcher);
            
            imageLoader.get(imageUrl, listener);
        }
    

    -->使用这个方法使得我们获取到网络中的图片

    四、分析与总结

    Volley的使用大大减少了我们异步获取网络数据中的代码,使得我们更快,更高效的得到从网络中取得数据

    五、相关jar包及链接

    5.1 Volley.jar   (http://yunpan.cn/cdYYMFt33Ky7d  访问密码 e9b4)

    5.2 Volley官方介绍 (https://developer.android.com/training/volley/index.html)

  • 相关阅读:
    记录——framework探测定位程序集与core探测定位程序集
    C# 特定框架适用特定代码
    python读取excel代码
    时间比较
    ORA 01791错误
    MongoDB.1什么是MongoDB
    Mayatis 异常之result maps collection already contains value...
    怎样做好黄焖鸡
    关于foreach
    C#之out,ref关键字
  • 原文地址:https://www.cnblogs.com/boy1025/p/4716697.html
Copyright © 2011-2022 走看看