Volley使用指南

1.什么是Volley

  在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient(Apache)等,今年的Google I/O 2013上,Volley发布了。Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮。

 视频:http://www.youtube.com/watch?v=y(需要翻墙)优酷:http://v.youku.com/v_show/id_XNTU4ODgzNjg4.html

 Volley提供的功能
简单来说,它提供了如下的便利功能:

  • Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.
  • Volley provides transparent disk and memory caching.
  • Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.
  • Volley provides powerful customization abilities.
  • Volley provides Debugging and tracing tools
2.使用Volley

从git库先克隆一个下来:git clone https://android.googlesource.com/platform/frameworks/volley

 将volley引入工程即可 

http请求

      RequestQueue  mQueue = Volley.newRequestQueue(getApplicationContext());
        String url = "https://api.instagram.com/v1/media/popular?client_id=e8e3eb77fcab4830947fed8677eeb6cd";
      
        JsonObjectRequest jsonRequet = new JsonObjectRequest(Method.GET, url,
                null, new Listener<JSONObject>() {
                    public void onResponse(JSONObject result) {
                       Log.i(TAG, "请求成功"+result);
                       
                    }
                }, new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "请求失败");
                    }
                });
        jsonRequet.setTag(TAG);
        mQueue.add(jsonRequet);

ImageView设置图片

RequestQueue  mQueue = Volley.newRequestQueue(getApplicationContext());
        imageview = (ImageView) findViewById(R.id.imageview);
        mImageLoader = new ImageLoader(mQueue, new ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>( 10 * 1024 * 1024){
                @Override
                protected int sizeOf(String key, Bitmap value) {
                    return value.getRowBytes() * value.getHeight();
                }
            };
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });
        String imageUrl="http://h.hiphotos.baidu.com/pic/w%3D230/sign=cec1af55a044ad342ebf8084e0a30c08/f11f3a292df5e0fec6c784b95d6034a85fdf72ae.jpg";
        ImageListener listener = ImageLoader.getImageListener(imageview, android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete);  
        mImageLoader.get(imageUrl, listener);

使用NetworkImageView

Volley提供了一个新的控件NetworkImageView来代替传统的ImageView,这个控件的图片属性可以通过setImageUrl方法来设置网络图片

 RequestQueue  mQueue = Volley.newRequestQueue(getApplicationContext());
        imageview = (NetworkImageView) findViewById(R.id.imageview);
        mImageLoader = new ImageLoader(mQueue, new ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>( 10 * 1024 * 1024){
                @Override
                protected int sizeOf(String key, Bitmap value) {
                    return value.getRowBytes() * value.getHeight();
                }
            };
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });
        String imageUrl="http://h.hiphotos.baidu.com/pic/w%3D230/sign=cec1af55a044ad342ebf8084e0a30c08/f11f3a292df5e0fec6c784b95d6034a85fdf72ae.jpg";
        
        imageview.setImageUrl(imageUrl, mImageLoader);

定制自己的Request

https://gist.github.com/ficusk/5474673

3.参考

http://blog.csdn.net/t12x3456/article/details/9221611

http://blog.csdn.net/geekpark/article/details/9380933

http://stackoverflow.com/questions/16626032/volley-post-get-parameters

http://stackoverflow.com/questions/17571759/looking-for-a-documentation-for-the-android-volley-api

(文档)http://files.evancharlton.com/volley-docs/

原文地址:https://www.cnblogs.com/malinkang/p/3293605.html