美文网首页程序员
Android 浅析 Volley (一) 使用

Android 浅析 Volley (一) 使用

作者: CodePlayer_Jz | 来源:发表于2016-03-21 21:09 被阅读194次

    Android 浅析 Volley (一) 使用


    前言

    Linus Benedict Torvalds : RTFSC – Read The Fucking Source Code

    概括

    Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster.
    Volley excels at RPC-type operations used to populate a UI, such as fetching a page of search results as structured data. It integrates easily with any protocol and comes out of the box with support for raw strings, images, and JSON. By providing built-in support for the features you need, Volley frees you from writing boilerplate code and allows you to concentrate on the logic that is specific to your app.

    使用

    Http请求

    Step 1.创建RequestQueue队列

    RequestQueue mQueue = Volley.newRequestQueue(this);
    通过newRequestQueue创建一个新的RequestQueue队列。

    Step 2.创建StringRequest对象

    StringRequest stringRequest = new StringRequest(
        "http://www.baidu.com",
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("TAG", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        }
    );
    

    这里可以看到StringRequest有三个默认参数,第一个是目标的Url,第二个是设置一个回调,第三个是监听错误回调的。

    Step 3.将quest对象添加到队列里

    mQueue.add(stringRequest);
    最后就是将StringRequest对象往StringRequest里面扔就行了。

    图片请求

    Step 1.创建RequestQueue对象

    RequestQueue mQueue = Volley.newRequestQueue(this);
    通过newRequestQueue创建一个新的RequestQueue队列。

    Step 2.创建ImageRequest对象

    mImageView = (ImageView) findViewById(R.id.imageView);
    ImageRequest imageRequest = new ImageRequest(
        "http://app.sjk.ijinshan.com/market/img/zs/2300841/20150805140347461.png",
        new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                mImageView.setImageBitmap(response);
            }
        }, 0, 0, 
        Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mImageView.setImageResource(R.drawable.default_image);
            }
        }
    );
    

    ImageRequest的构造函数接收六个参数,第一个参数就是图片的URL地址。第二个参数是图片请求成功的回调,这里我们把返回的Bitmap参数设置到ImageView中。第三第四个参数分别用于指定允许图片最大的宽度和高度,如果指定的网络图片的宽度或高度大于这里的最大值,则会对图片进行压缩,指定成0的话就表示不管图片有多大,都不会进行压缩。第五个参数用于指定图片的颜色属性,Bitmap.Config下的几个常量都可以在这里使用,其中ARGB_8888可以展示最好的颜色属性,每个图片像素占据4个字节的大小,而RGB_565则表示每个图片像素占据2个字节大小。第六个参数是图片请求失败的回调,这里我们当请求失败时在ImageView中显示一张默认图片。

    Step 3.将quest对象添加到队列里

    mQueue.add(stringRequest);
    最后就是将StringRequest对象往StringRequest里面扔就行了。

    自定义Quest请求

    自定义类

    自定义的请求其实很简单,首先是继承父类Request<T>,然后重载其中两个函数parseNetworkResponse(NetworkResponse response),deliverResponse(XmlPullParser response)。就可以像用StringResponse那样去使用了。

    总结

    Volley总体来说非常好用和方便,封装好了各种网络请求,特别是底层支持对开发者来说免除了很多烦恼,还可以定制自己的Response。

    相关文章

      网友评论

        本文标题:Android 浅析 Volley (一) 使用

        本文链接:https://www.haomeiwen.com/subject/epgklttx.html