咱们平时在开发Android应用的时候,不可避免地常常要经过网络来进行数据的收发,而多数状况下都是会用HTTP协议来作这些事情。Android系统主要提供了HttpURLConnection和Apache HttpClient这两种方式来帮咱们进行HTTP通讯。对于这两种方式,Google官方的一份文档 Android’s HTTP Clients 有作一个对比说明。是说,Apache HttpClient提供的API很是多,实现稳定,bug也比较少,正因如此,为了保持API兼容性而很是难以作优化。HttpURLConnection的API比较少,故而比较容易作优化。但在Android 2.3以前,HttpURLConnection的实现又有一些比较严重的问题。Google官方建议在2.2及以前的Android上,用Apache HttpClient来执行HTTP请求,在2.3及以后的Android上,则用HttpURLConnection接口。php
另外,HttpURLConnection和HttpClient的用法仍是有些复杂的,提供的功能也比较基础,若是不进行适当封装的话,很容易写出大量重复代码。因而乎,一些Android网络通讯框架也就应运而生,好比说AsyncHttpClient等,它把HTTP全部的通讯细节所有封装在内部,同时提供更为强大的API,咱们只需简单调用几行代码就能够完成通讯操做。html
Volley是Google提供的一个HTTP网络库,其功能大致是提供对通讯细节的封装,以方便网络操做的调用,volley在内部实现中,会根据运行的android的版本,来决定是使用HttpURLConnection和Apache HttpClient接口;提供缓存机制,以加速网络访问;提供HTTP请求异步执行的能力。这里咱们就来看一下Volley的设计和实现。java
咱们先来了解一些怎么下载到volley的代码。咱们能够经过以下的命令,下载的volley的代码:android
git clone https://android.googlesource.com/platform/frameworks/volley
下载了volley以后,将代码导入到Android Studio中,根据volley工程的配置对于工具版本的要求,下载必要的工具,好比Android SDK platform,SDK Build tools,Gradle插件,或者根据本地工具链的版本,适当修改volley工程的设置,随后就能够对volley进行编译,产生aar包了。git
这里咱们经过一个简单的例子来看一下volley的使用。好比,咱们利用淘宝的接口抓取某一个IP地址的相关信息:shell
public class MainActivity extends AppCompatActivity { private static final String TAG = "myapplication"; private TextView mWeatherDataText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWeatherDataText = (TextView)findViewById(R.id.weather_data); getIpData(); } private void getIpData() { String RegionServiceUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=112.65.189.212"; RequestQueue requestQueue = Volley.newRequestQueue(this); StringRequest request = new StringRequest(RegionServiceUrl, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i(TAG, "response = " + response); mWeatherDataText.setText(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i(TAG, "error = " + error.getMessage()); mWeatherDataText.setText(error.getMessage()); } }); requestQueue.add(request); } }
主要关注getIpData(),实际是在这个方法中利用volley,执行了网络请求。能够看到,使用volley执行网络请求,大概分为以下的几个步骤:数组
经过volley执行基本的网络请求就是这么简单。要执行更复杂的网络请求的话,能够自行探索。缓存
这里咱们以2016.5.27 clone下来的代码为基础进行volley整个设计与实现的分析。咱们先来看一下Volley的代码结构:网络
com/android/volley/AuthFailureError.java com/android/volley/Cache.java com/android/volley/CacheDispatcher.java com/android/volley/ClientError.java com/android/volley/DefaultRetryPolicy.java com/android/volley/ExecutorDelivery.java com/android/volley/Network.java com/android/volley/NetworkDispatcher.java com/android/volley/NetworkError.java com/android/volley/NetworkResponse.java com/android/volley/NoConnectionError.java com/android/volley/ParseError.java com/android/volley/Request.java com/android/volley/RequestQueue.java com/android/volley/Response.java com/android/volley/ResponseDelivery.java com/android/volley/RetryPolicy.java com/android/volley/ServerError.java com/android/volley/TimeoutError.java com/android/volley/VolleyError.java com/android/volley/VolleyLog.java com/android/volley/toolbox/AndroidAuthenticator.java com/android/volley/toolbox/Authenticator.java com/android/volley/toolbox/BasicNetwork.java com/android/volley/toolbox/ByteArrayPool.java com/android/volley/toolbox/ClearCacheRequest.java com/android/volley/toolbox/DiskBasedCache.java com/android/volley/toolbox/HttpClientStack.java com/android/volley/toolbox/HttpHeaderParser.java com/android/volley/toolbox/HttpStack.java com/android/volley/toolbox/HurlStack.java com/android/volley/toolbox/ImageLoader.java com/android/volley/toolbox/ImageRequest.java com/android/volley/toolbox/JsonArrayRequest.java com/android/volley/toolbox/JsonObjectRequest.java com/android/volley/toolbox/JsonRequest.java com/android/volley/toolbox/NetworkImageView.java com/android/volley/toolbox/NoCache.java com/android/volley/toolbox/PoolingByteArrayOutputStream.java com/android/volley/toolbox/RequestFuture.java com/android/volley/toolbox/StringRequest.java com/android/volley/toolbox/Volley.java
能够看到volley的全部代码都在两个package中,一个是com.android.volley,另外一个是com.android.volley.toolbox,前者能够认为是定义了volley的框架架构及接口,然后者则是相关接口的实现,提供实际的诸如HTTP网络访问、缓存等功能。架构
这里先分析com.android.volley包,来看一下volley总体的框架架构。com.android.volley包中,类名以Error结尾的全部类都是Exception,用来指示某种异常。全部这些类的层次结构以下图:
对于这些Exception类,没有须要过多说明的地方。接下来,咱们从网络请求的执行及执行结果的发布的角度来看一下com.android.volley包中各个类之间的关系,以下图:
如咱们在上面 Volley的使用 一节中看到的,应用程序在建立了Request以后,会将这个Request丢给RequestQueue,RequestQueue负责这个Request的处理及结果的Post。
RequestQueue在拿到Request以后,会根据这个Request是否应该缓存而将这个Request丢进NetworkQueue或CacheQueue,若Request应该缓存它会被放进CacheQueue中,若不须要则会被直接放进NetworkQueue中。NetworkQueue和CacheQueue都是类型为PriorityBlockingQueue<Request<?>>的容器。
NetworkDispatcher和CacheDispatcher都是Thread。NetworkDispatcher主要的职责是经过Network执行HTTP请求并抛出执行结果。NetworkDispatcher线程在被启动以后,会不断地从NetworkQueue中取出Request来执行,执行以后获得NetworkReponse,NetworkReponse会获得解析并被从新构造为Response,构造后的Response会被丢给ResponseDelivery,并由后者发布给volley的调用者,同时在Request应该被缓存时,得到的Response数据还会被放进Cache中。在Volley中,会建立NetworkDispatcher线程的线程池,其中包含固定的4个线程。
CacheDispatcher的主要职责则是访问缓存,找到以前缓存的下载的数据,并经过ResponseDelivery发布给volley的调用者,在没找到时,则将Request丢进NetworkQueue中,以便于从网络中获取。在Volley中,只有一个CacheDispatcher线程。
Cache主要定义了缓存接口。RetryPolicy定义了缓存策略的接口,每一个Request都会有本身的RetryPolicy,用于帮助Network肯定重试的策略。ResponseDelivery定义了Request的发布者的行为,ExecutorDelivery是ResponseDelivery的一个实现,它主要是将结果post到一个Executor中。
接下来经过代码来看一下Volley的实现。
在Volley中,主要经过Volley类的newRequest来建立RequestQueue对象。Volley类就像胶水同样,把Network的实现BasicNetwork/HurlStack/HttpClientStack和Cache的实现DiskBasedCache粘到一块儿,建立出可用的RequestQueue。其代码以下:
public class Volley { /** Default on-disk cache directory. */ private static final String DEFAULT_CACHE_DIR = "volley"; /** * 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()方法就是咱们前面用到的那个,它会直接传入null HttpStack调用带HttpStack参数的newRequestQueue()方法。带参数的newRequestQueue()方法的实现,感受改成下面这样彷佛更加清晰一点:
public static RequestQueue newRequestQueue(Context context, HttpStack stack) { if (stack == null) { if (Build.VERSION.SDK_INT >= 9) { stack = new HurlStack(); } else { String userAgent = "volley/0"; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); userAgent = packageName + "/" + info.versionCode; } catch (NameNotFoundException e) { } // 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); File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR); RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network); queue.start(); return queue; }
以上面的这段代码为基础,来分析newRequestQueue()方法作的事情。能够看到它主要作了以下这样几件事情:
public interface HttpStack { /** * Performs an HTTP request with the given parameters. * * A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise, * and the Content-Type header is set to request.getPostBodyContentType(). * * @param request the request to perform * @param additionalHeaders additional headers to be sent together with * {@link Request#getHeaders()} * @return the HTTP response */ public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError; }
HurlStack的对象建立过程:
/** * An interface for transforming URLs before use. */ public interface UrlRewriter { /** * Returns a URL to use instead of the provided one, or null to indicate * this URL should not be used at all. */ public String rewriteUrl(String originalUrl); } private final UrlRewriter mUrlRewriter; private final SSLSocketFactory mSslSocketFactory; public HurlStack() { this(null); } /** * @param urlRewriter Rewriter to use for request URLs */ public HurlStack(UrlRewriter urlRewriter) { this(urlRewriter, null); } /** * @param urlRewriter Rewriter to use for request URLs * @param sslSocketFactory SSL factory to use for HTTPS connections */ public HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) { mUrlRewriter = urlRewriter; mSslSocketFactory = sslSocketFactory; }
而后是HttpClientStack对象的建立过程:
protected final HttpClient mClient; private final static String HEADER_CONTENT_TYPE = "Content-Type"; public HttpClientStack(HttpClient client) { mClient = client; }
protected final HttpStack mHttpStack; protected final ByteArrayPool mPool; /** * @param httpStack HTTP stack to be used */ public BasicNetwork(HttpStack httpStack) { // If a pool isn't passed in, then build a small default pool that will give us a lot of // benefit and not use too much memory. this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE)); } /** * @param httpStack HTTP stack to be used * @param pool a buffer pool that improves GC performance in copy operations */ public BasicNetwork(HttpStack httpStack, ByteArrayPool pool) { mHttpStack = httpStack; mPool = pool; }
/** Default maximum disk usage in bytes. */ private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024; /** High water mark percentage for the cache */ private static final float HYSTERESIS_FACTOR = 0.9f; /** Magic number for current version of cache file format. */ private static final int CACHE_MAGIC = 0x20150306; /** * Constructs an instance of the DiskBasedCache at the specified directory. * @param rootDirectory The root directory of the cache. * @param maxCacheSizeInBytes The maximum size of the cache in bytes. */ public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) { mRootDirectory = rootDirectory; mMaxCacheSizeInBytes = maxCacheSizeInBytes; } /** * Constructs an instance of the DiskBasedCache at the specified directory using * the default maximum cache size of 5MB. * @param rootDirectory The root directory of the cache. */ public DiskBasedCache(File rootDirectory) { this(rootDirectory, DEFAULT_DISK_USAGE_BYTES); }
能够看到,volley建立了一个最大大小为5MB的一个基于磁盘的缓存,缓存目录的位置为application的缓存目录。 4. 传递BasicNetwork对象和DiskBasedCache对象,构造RequestQueue对象。 5. 执行RequestQueue的start()方法,启动Request内部的线程。 总体地来看一下RequestQueue对象的构造,和start()初始化过程:
/** Number of network request dispatcher threads to start. */ private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 4; /** Cache interface for retrieving and storing responses. */ private final Cache mCache; /** Network interface for performing requests. */ private final Network mNetwork; /** Response delivery mechanism. */ private final ResponseDelivery mDelivery; /** The network dispatchers. */ private NetworkDispatcher[] mDispatchers; /** The cache dispatcher. */ private CacheDispatcher mCacheDispatcher; private List<RequestFinishedListener> mFinishedListeners = new ArrayList<RequestFinishedListener>(); /** * 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 * @param threadPoolSize Number of network dispatcher threads to create * @param delivery A ResponseDelivery interface for posting responses and errors */ public RequestQueue(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) { mCache = cache; mNetwork = network; mDispatchers = new NetworkDispatcher[threadPoolSize]; mDelivery = delivery; } /** * 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 * @param threadPoolSize Number of network dispatcher threads to create */ 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); } /** * Starts the dispatchers in this queue. */ 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(); } } /** * Stops the cache and network dispatchers. */ public void stop() { if (mCacheDispatcher != null) { mCacheDispatcher.quit(); } for (int i = 0; i < mDispatchers.length; i++) { if (mDispatchers[i] != null) { mDispatchers[i].quit(); } } }
在RequestQueue对象的构造过程当中,会建立ExecutorDelivery对象,该对象被用于发布网络请求的执行结果,向application的主UI线程中发布,后面咱们分析结果发布时,会更详细地来分析这个类。还会建立一个NetworkDispatcher的数组,其中包含了4个元素,也便是说,volley的网络请求是经过后台一个含有4个线程的固定线程池来执行的。 在RequestQueue的start()方法中,则主要是清理掉老的CacheDispatcher和NetworkDispatcher线程,建立新的并启动他们。
这里经过RequestQueue.add()的代码,来具体看一下,向RequestQueue中添加一个Request的执行过程:
/** * Adds a Request to the dispatch queue. * @param request The request to service * @return The passed-in request */ 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; } }
能够看到RequestQueue.add()为Request设置了RquestQueue。