在Volley类中创建RequestQueue的时候,Volley就会根据设备的SDK版本来创建不同的HttpStack接口实现类,分别是HurlStack和HttpClientStack。
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
而在前面的章节中讲到,在BasicNetwork中,其实真正去跟网络打交道的正是这两个对象,在BasicNetwork, 调用HttpStack的performRequest方法,
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
...
httpResponse = mHttpStack.performRequest(request, headers);
由于HurlStack和HttpClientStack的实现机制是一样的,只是使用的类不一样,我们这篇文章就只讲解HurlStack了。
下面是HurlStack中performRequest方法,
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());//默认为null
map.putAll(additionalHeaders);//添加头部,主要是缓存相关的头部信息
if (mUrlRewriter != null) {
...//代码不执行
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);//打开Connection
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}//将Map的对象添加到Connection的属性中
setConnectionParametersForRequest(connection, request);//设置connection方法,主要是设置Method属性和Content(for post/put)
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);//Http 1.1 协议
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));将返回的内容解析成response的Entity对象
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
HttpURLConnection是Android3.0以后才提供的一个网络访问类,而HurlStack类,也正是H(ttp)URL的缩写,所以这个类,其实就是基于HttpUrlConnection的实现,其步骤如下:
1)从Request中获得url参数,根据url参数构造URL对象,而URL对象是java提供的获取网络资源的一个封装好的实用类。
2)从URL对象打开Connection,并设置connection的超时,缓存,让网络资源写入等属性。
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
HttpURLConnection connection = createConnection(url);
int timeoutMs = request.getTimeoutMs();
connection.setConnectTimeout(timeoutMs);
connection.setReadTimeout(timeoutMs);
connection.setUseCaches(false);
connection.setDoInput(true);
// use caller-provided custom SslSocketFactory, if any, for HTTPS
if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
}
return connection;
}
3)调用方法setConnectionParametersForRequest 来设置 Method属性,如果是Post或者Put的话,还要设置Content内容。
4)设置Http 协议,这里基本上是1.1了。
5)获得Response的流,并将其解析成对应的HttpEntity对象,设置给Response.entity字段,返回给BasicNetwork。
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
6)BasicNetwork获得返回来的Response对象,就会由Request去解析这个Response对象,因为不同的请求返回来的对象是不一样的,所以这个解析的过程必须由各个请求的实现类自己去实现,也即如ImageRequest,JsonObjectRequest对象等,都要实现自己的parseNetworkResponse方法。
HurlStack提供了三个构造函数,如下:
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;
}
其中第一个就是Volley类中使用的构造函数,但其实最终调用的都是
public HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) {
mUrlRewriter = urlRewriter;
mSslSocketFactory = sslSocketFactory;
}
默认这两个类都是为null的,但是如果我们要实现对Url的拦截,对url进行一些处理的话,或者利用Https来保证数据传输的安全性的话,我们就可以传入自己实现的UrlRewriterc对象,或者添加SSlSocketFactory。不过在我们一般的项目中,也是用不到的啦,只要稍微了解一下就好了。
结束。
分享到:
相关推荐
Android中关于Volley的使用,讲解很详细,需要的请下载
android最新网络框架Volley使用实例
com.android.volley:volley volley.jar
Android网络框架-Volley(一) 初步使用Volley
Android之Volley框架的使用,包括Volley的get和post请求方式的使用、Volley的取消队列网络请求和Volley与Activity生命周期的联动、Volley的简单的二次回调封装、Volley加载图片的介绍及用法
介绍了Volley的基本使用方式,也可以看文章介绍。
Android Volley 支持多个volley
Volley是一个由Google官方推出的网络通信库,它使得Android进行网络请求时更加方便、快速、健壮,同时对网络图片加载也提供了良好的支持。
3. Android Tipshttp://dev.classmethod.jp/smartphone/android/android-tips-51-volley/ 4. Google I/O 2013 – Android : Volley: Easy, Fast Networking for Android ...
android-volley源码代码 android Volley ---------- This is an unofficial mirror for [android volley library](https://android.googlesource.com/platform/frameworks/volley), the source code will ...
android-volley-master volley library。android开发之Google官方开源框架,著名的开源框架
1.发送get 2.发送post 3.获取图片 4.发送https 5.获取cookie 6.上传cookie 7.自定义request 8.设置超时
参考: Android中关于Volley的使用(八)缓存机制的深入认识
Android开发-Volley-解析Json使用方法-完整Demo-AndroidStudio http://blog.csdn.net/iwanghang/article/details/52241233
今天学习使用了网络加载库Volley,先说下Volley库的地址: https://github.com/mcxiaoke/android-volley。这里是一个VolleyDemo。
Android单元测试,结合一些测试框架(如:Robolectric、Mockito等)与Volley网络请求的单元测试
android volley 安卓资源jar包,下载吧。
Android Volley Jar,Android Volley框架是android开发中不可或缺的一个重要框架,想使用Android开发更加便捷的途径就是使用Volley框架了。 Android Volley Jar框架简介 我们平时在开发Android应用的时候不可避免...
android volley
使用Volley StringRequest和JsonObjectRequest遇到的一些问题。博客地址:http://blog.csdn.net/onlysnail/article/details/47905375