• 快速Android开发系列网络篇之Volley


    Volley是Google推出的一个网络请求库,已经被放到了Android源码中,地址在这里,先看使用方法

    RequestQueue mRequestQueue = Volley.newRequestQueue(context);
    JsonObjectRequest req = new JsonObjectRequest(URL, null,
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   try {
                       VolleyLog.v("Response:%n %s", response.toString(4));
                   } catch (JSONException e) {
                       e.printStackTrace();
                   }
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.e("Error: ", error.getMessage());
               }
           });
    mRequestQueue.add(req);

     详细的使用方法就不说了,网上很多,可以看下这个,这里只大概介绍一下Volley的工作方法,就从上面的例子开始。

    我们接触到的Volley的核心就两个,从名字就可以看出其用途。

    • RequestQueue
    • Request

    前面我们看到RequestQueue是通过Volley的方法newRequestQueue获得的,Volley类的唯一作用就是获取RequestQueue的实例,而我们完全可以自己new RequestQueue,不知道为什么不把这两个类合并了。

    /**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @param stack An {@link HttpStack} to use for the network, or null for default.
     * @return A started {@link RequestQueue} instance.
     */
    public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
    
        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }
    
        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }
    
        Network network = new BasicNetwork(stack);
    
        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        queue.start();
    
        return queue;
    }
    
    /**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @return A started {@link RequestQueue} instance.
     */
    public static RequestQueue newRequestQueue(Context context) {
        return newRequestQueue(context, null);
    }

     HttpStack

    在newRequestQueue里出现了几个重要的概念,首先可以看到newRequestQueue有一个重载方法,接收一个HttpStack的实例,HttpStack只有一个方法:performRequest,用来执行网络请求并返回HttpResponse,如果不传这个参数就根据API Level自己选择:

    • 当 API >= 9 即2.3及以后的系统使用HurlStack
    • 2.3以前的系统使用HttpClientStack

    从这两个类的名字就大概知道了它们的区别了:HurlStack内部使用HttpURLConnection执行网络请求,HttpClientStack内部使用HttpClient执行网络请求,至于为什么么这样,可以自备梯子看这篇文章

    Network

    Network是请求网络的接口,只有一个实现类BasicNetwork,只有一个方法performRequest,执行Request返回NetworkResponse。

    NetworkHttpStack接口都只有一个方法,从方法的名字就可以看出它们的区别,Network.performRequest收Request参数返回om.android.volley.NetworkResponseHttpStack.performRequest返回org.apache.http.HttpResponse,层次更低,所以应该是Network.performRequest中调用HttpStack.performRequest执行实际的请求,并将HttpStack.performRequest返回的org.apache.http.HttpResponse封装成com.android.volley.NetworkResponse返回。

    Cache

    Volley中使用Cache接口的子类DiskBasedCache做缓存,这是一个文件缓存,Cache接口有一个initialize方法用来初始化缓存,这个方法可能会执行耗时操作,需要在后台线程中执行,看DiskBasedCache可以知道,当它将缓存写到文件时,在文件的头部写了一些Header信息,在initialize时就会将这些Header信息读入内存中。

    Request类中有一个方法叫parseNetworkResponseRequest的子类会覆写这个方法解析网络请求的结果,在这个方法中会调用

    return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));

     返回Response<T>,并通过HttpHeaderParse.parseCacheHeaders解析Cache.Entity,即生成缓存对象,在parseaCheHeaders中会根据网络请求结果中的Header中的ExpiresCache-Control等信息判断是否需要缓存,如果不需要就返回null不缓存。

    当对请求做了缓存后,没网的情况下也可以得到数据。

    Cache还有一个子类叫NoCache,get方法返回Null,其他方法都是空的,所以使用NoCache就表示不用缓存。

    RequestQueue

    public RequestQueue(Cache cache, Network network, int threadPoolSize) {
    this(cache, network, threadPoolSize,
                new ExecutorDelivery(new Handler(Looper.getMainLooper())));
    }
    
    /**
     * Creates the worker pool. Processing will not begin until {@link #start()} is called.
     *
     * @param cache A Cache to use for persisting responses to disk
     * @param network A Network interface for performing HTTP requests
     */
    public RequestQueue(Cache cache, Network network) {
        this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
    }
    
    public void start() {
        stop();  // Make sure any currently running dispatchers are stopped.
        // Create the cache dispatcher and start it.
        mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
        mCacheDispatcher.start();
    
        // Create network dispatchers (and corresponding threads) up to the pool size.
        for (int i = 0; i < mDispatchers.length; i++) {
            NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
                    mCache, mDelivery);
            mDispatchers[i] = networkDispatcher;
            networkDispatcher.start();
        }
    }

     RequestQueue是请求队列,负责分发请求,取缓存或读网络,所以其构造函数中需要一个Cache对象和一个Network对象,还有一个ResponseDelivery对象用于派发结果。

    新建RequestQueue后要调用它的start方法,在start中会新建一个CacheDispatcher和几个NetworkDispatcher分别处理缓存与网络请求

    通过RequestQueue的add方法添加请求:

    public <T> Request<T> add(Request<T> request) {
        // Tag the request as belonging to this queue and add it to the set of current requests.
        request.setRequestQueue(this);
        synchronized (mCurrentRequests) {
            mCurrentRequests.add(request);
        }
    
        // Process requests in the order they are added.
        request.setSequence(getSequenceNumber());
        request.addMarker("add-to-queue");
    
        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache()) {
            mNetworkQueue.add(request);
            return request;
        }
    
        // Insert request into stage if there's already a request with the same cache key in flight.
        synchronized (mWaitingRequests) {
            String cacheKey = request.getCacheKey();
            if (mWaitingRequests.containsKey(cacheKey)) {
                // There is already a request in flight. Queue up.
                Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
                if (stagedRequests == null) {
                    stagedRequests = new LinkedList<Request<?>>();
                }
                stagedRequests.add(request);
                mWaitingRequests.put(cacheKey, stagedRequests);
                if (VolleyLog.DEBUG) {
                    VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
                }
            } else {
                // Insert 'null' queue for this cacheKey, indicating there is now a request in
                // flight.
                mWaitingRequests.put(cacheKey, null);
                mCacheQueue.add(request);
            }
            return request;
        }
    }

     add方法有以下几个步骤:

    1. 判断当前的Request是否使用缓存,如果不使用缓存直接加入网络请求队列mNetworkQueue返回
    2. 如果使用缓存,判断之前是否有执行相同的请求且还没有返回结果
    3. 如果第2步的判断是true,将此请求加入mWaitingRequests队列,不再重复请求,在上一个请求返回时直接发送结果
    4. 如果第2步的判断是false,将请求加入缓存队列mCacheQueue,同时加入mWaitingRequests中用来当下个请求来时做第2步中的判断

    RequestQueue.add的任务就是这些,可以看到,它并没有执行任何实际的请求操作,包括判断缓存与请求网络,直正的操作是接下来要说的两个类执行的。

    CacheDispatcher

    mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);

     在RequestQueue.add方法中,如果使用缓存直接就将Request放入缓存队列mCacheQueue中了,使用mCacheQueue的位置就是CacheDispatcher,CacheDispatcher的构造函数中传入了缓存队列mCacheQueue、网络队列mNetworkQueue、缓存对象mCache及结果派发器mDelivery。

    CacheDispatcher继承自Thread,当被start后就执行它的run方法,代码不贴了,主要完成以下工作:

    1. 从mCacheQueue取请求Request
    2. 每个Request都可以从中得到CacheKey,看对应的CacheKey在缓存mCache中是否存在
    3. 如果缓存不存在就加到网络队列mNetworkQueue中继续取下一个请求
    4. 如果缓存存在,判断是否过期
    5. 如果过期了就加入网络队列mNetworkQueue中继续取下一个请求
    6. 如果没过期,看是否需要刷新
    7. 如果不需要刷新,直接派发结果
    8. 如果需要刷新,调用mDelivery.postResponse派发结果,并将Request加入网络队列重新请求最新数据
    response.intermediate = true;
    // Post the intermediate response back to the user and have
    // the delivery then forward the request along to the network.
    mDelivery.postResponse(request, response, new Runnable() {
        @Override
        public void run() {
            try {
                mNetworkQueue.put(request);
            } catch (InterruptedException e) {
                // Not much we can do about this.
            }
        }
    });

    NetworkDispatcher

    NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);

     NetworkDispatcher的工作方法同CacheDispatcher一样,继承自Thread,当被start后,不停地从mNetworkQueue取请求,然后通过Network接口请求网络。

    当拿到请求结果后,如果服务器返回304(自上次请求后结果无变化)并且结果已经通过缓存派发了(即这次是读了缓存后的Refresh),那么什么也不做,否则调用Request的parseNetworkResponse解析请求结果,如果需要进行缓存,并派发结果

    ResponseDelivery

    派发请求结果的接口,有一个子类ExecutorDelivery执行实际操作,构造ExecutorDelivery的对象时需要一个Handler对象,当向ExecutorDelivery请求派发结果时会向这个Handler post消息。

    Request

    Request表示一个请求,支持四种优先级:LOW、NORMAL、HIGH、IMMEDIATE,主要有以下几个方法:

    • getHeaders 获取请求Http Header列表
    • getBodyContentType 请求类型,如application/x-www-form-urlencoded; charset=utf-8
    • getBody 将要发送的POST或PUT请求的内容
    • getParams,获取POST或PUT请求的参数,如果重写getBody的话这个就用不到了
    • parseNetworkResponse 将请求结果解析成需要的类型,将NetworkResponse解析成Response<T>,NetworkResponse中的data成员即网络请求结果为byte[]
    • deliverResponse 子类需要实现,用于将结果派发至Listener

    StringRequest将结果转换成了String并Deliver至Response.Listener

    @Override
    protected void deliverResponse(String response) {
        mListener.onResponse(response);
    }
    
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

     JsonRequest<T>

    可以在发送请求时同时发送一个JSONObject参数,覆写了getBody

    @Override
    public byte[] getBody() {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, PROTOCOL_CHARSET);
            return null;
        }
    }

     mRequestBody是构造函数里传进来的一个String,一般是JSONObject.toString()。

    JsonObjectRequest

    继承自JSONRequest<T>,将请求结果解析成JSONObject

    public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
            Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                    errorListener);
    }
    
    @Override
    protected Response<JSONObject> rkResponse(NetworkResponse response) {
        try {
            String jsonString =
                new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

     JsonArrayRequest

    同JsonObjectRequest一样,继承自JSONRequest<T>,只是把结果解析成JSONArray。

    ClearCacheRequest

    Hack性质的请求,用于清除缓存,设置为最高优先级IMMEDIATE,执行请求时会调用Request.isCanceled判断请求是否被取消掉了,就在这里清除了缓存

    @Override
    public boolean isCanceled() {
        // This is a little bit of a hack, but hey, why not.
        mCache.clear();
        if (mCallback != null) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postAtFrontOfQueue(mCallback);
        }
        return true;
    }

    Cancel

    RequestQueue同样提供了取消请求的方法。通过它的cancelAll方法。

    /**
     * A simple predicate or filter interface for Requests, for use by
     * {@link RequestQueue#cancelAll(RequestFilter)}.
     */
    public interface RequestFilter {
        public boolean apply(Request<?> request);
    }
    
    /**
     * Cancels all requests in this queue for which the given filter applies.
     * @param filter The filtering function to use
     */
    public void cancelAll(RequestFilter filter) {
        synchronized (mCurrentRequests) {
            for (Request<?> request : mCurrentRequests) {
                if (filter.apply(request)) {
                    request.cancel();
                }
            }
        }
    }
    
    /**
     * Cancels all requests in this queue with the given tag. Tag must be non-null
     * and equality is by identity.
     */
    public void cancelAll(final Object tag) {
        if (tag == null) {
            throw new IllegalArgumentException("Cannot cancelAll with a null tag");
        }
        cancelAll(new RequestFilter() {
            @Override
            public boolean apply(Request<?> request) {
                return request.getTag() == tag;
            }
        });
    }

     通过给每个请求设置一个Tag,然后通过cancelAll(final Object tag)就可以取消对应Tag的请求,也可以直接使用RequestFilter

    图片加载

    通过ImageRequest、ImageLoader和NetworkImageView等类,Volley还可用于加载图片,通过加锁实现了同时只解析一张图片,同时只解析一张,而不是只加载一张,网络请求还是跟普通的请求一样,返回的是byte数组,解析指byte[]->Bitmap,由于请求结果是byte[],大图应该很容易内存溢出,而且不支持本地图片,所以不考虑使用,略过。

    总结

    Volley的扩展应该还是比较容易的,网络已经有各种版本扩展了,像Cache,Request等都是提供的接口,很容易有自己的实现,比如实现GsonRequest用于使用Gson解析返回的json结果:

    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
                    response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(
                    gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));
        }
    }

     Volley被设计用于小的网络请求,所以像上传下载大文件什么的就不适合了,虽然网上已有相应的扩展,而且原生是没有文件上传的。

    Volley还有NetImageView,ImageLoader等和加载图片相关的,不过我个人习惯了使用UniversalImageLoader

    虽然网上都说Volley速度快,易于扩展,也给出了对比数据,但总归是要自己手工扩展,也没看出特别大的优势,和Android-Async-Http相比有一点不同就是2.3后使用了官方建议的HttpUrlConnection

    还有一点最主要的应该就是Volley的缓存方法了,根据进行请求时服务器返回的缓存控制Header对请求结果进行缓存,下次请求时判断如果没有过期就直接使用缓存加快响应速度,如果需要会再次请求服务器进行刷新,如果服务器返回了304,表示请求的资源自上次请求缓存后还没有改变,这种情况就直接用缓存不用再次刷新页面,不过这要服务器支持了。

    当对上次的请求进行缓存后,在下次请求时即使没有网络也可以请求成功,关键的是,缓存的处理对用户完全是透明的,对于一些简单的情况会省去缓存相关的一些事情

  • 相关阅读:
    中国内地、台湾、香港、澳门和国外DNS服务器地址列表
    科学、道法、哲学
    Away 3d 基本属性
    away 3d的一些问题
    Adobe Flash CC 2014 下载及破解
    html5结合flash实现视频文件在所有主流浏览器兼容播放
    Html wmode 标签参数详解
    九宫格
    flash/flex 编译错误汇总
    Redis在windows下安装过程(转)
  • 原文地址:https://www.cnblogs.com/angeldevil/p/3735051.html
Copyright © 2020-2023  润新知