Volley 核心源码解析(一)

[size=large][color=orange]Http 请求的基本过程[/color][/size]

volley 的 初始化 RequestQueue requestQueue = Volley.newRequestQueue(context),

返回的是一个 RequestQueue 对象,这个对象的作用下面再说。现在进入到方法的内部可以看到

这么几个重载的方法:

public static RequestQueue newRequestQueue(Context context) {
return newRequestQueue(context, null);
}

public static RequestQueue newRequestQueue(Context context, HttpStack stack)
{
return newRequestQueue(context, stack, -1);
}

[b]public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {
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
[color=red] stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));[/color]
}
}

[color=red] Network network = new BasicNetwork(stack);[/b][/color]


此时我们看看 HttpClientStack类,那么这个类是做什么的呢?

[b]public class HttpClientStack implements HttpStack[/b]

HttpStack 是一个接口 仅仅只有一个方法:

[b]public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError;[/b]
[align=left][/align]
参数是一个Request<?>的对象 这是一个泛型,它的作用下面再说。
那么这个方法译为中文就是:执行请求。

在HttpClientStack 中 performRequest的具体实现如下:

[b] @Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
[color=red] HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);[/color]
addHeaders(httpRequest, additionalHeaders);
addHeaders(httpRequest, request.getHeaders());
onPrepareRequest(httpRequest);
HttpParams httpParams = httpRequest.getParams();
int timeoutMs = request.getTimeoutMs();
// TODO: Reevaluate this connection timeout based on more wide-scale
// data collection and possibly different for wifi vs. 3G.
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
return [color=red]mClient[/color].execute(httpRequest);
}[/b]

protected final HttpClient mClient;

直到这时候,才发现Volley 默认使用的是apache Http Client框架,记得很早以前看到一篇文章说Android开发团队 推荐使用java.net 包中的HttpUrlConnection作为网络请求的方式,针对android 平台做过优化,却不知为何在Volley中 还是默认使用HttpClient?

[b]再来看看Network 接口;[/b]

public NetworkResponse performRequest(Request<?> request) throws VolleyError;

仍然只有一个执行请求的方法。

在实现类BasicNetwork 的构造方法中,HttpStack 被作为了参数。

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
Map<String, String> responseHeaders = Collections.emptyMap();
try {
// Gather headers.
Map<String, String> headers = new HashMap<String, String>();
addCacheHeaders(headers, request.getCacheEntry());
[color=red][b]httpResponse = mHttpStack.performRequest(request, headers);[/b][/color]
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();

.....
}

看到这里 我们似乎明白了volley 对http请求的调用顺序是:

[b]Network ----> HttpStack---->HttpClient[/b]


如果读者细心会发现,现有的网络执行过程中,不支持上传文件等属性,
在Volley 初始化 请求队列的时候,有一个重载方法:
public static RequestQueue newRequestQueue(Context context, [b]HttpStack stack[/b])
{
return newRequestQueue(context, stack, -1);
}

那么要实现更复杂的网络请求, 只需要实现自己的HttpStack 在
performRequest() 方法中灵活实现即可满足需求。


[b]Volley 的 Request对象 [/b]


阅读源代码 我们发现
public abstract class Request<T> implements Comparable<Request<T>>

Request 是一个泛型的抽象类,且是可比较的。

[b]那么为什么要实现 Comparable接口呢?[/b]

我的理解是要避免重复请求。


public interface Method {
int DEPRECATED_GET_OR_POST = -1;
int GET = 0;
int POST = 1;
int PUT = 2;
int DELETE = 3;
int HEAD = 4;
int OPTIONS = 5;
int TRACE = 6;
int PATCH = 7;
}


在Request中 有Method接口 定义了支持的Http请求方法。

[b]abstract protected Response<T> parseNetworkResponse(NetworkResponse response);[/b]
这是一个抽象方法,那么要实现自定义的请求 就必须实现这个方法。如GsonRequest,ImageRequest等等。

发送Post请求必须重写getPostParams()方法:

@Deprecated
protected Map<String, String> getPostParams() throws AuthFailureError {
return getParams();
}


至此,对Volley 的网络请求的过程以及源码的分析 告一段落。


下一节 请求队列 RequestQueue [url]http://f303153041.iteye.com/blog/2281350[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值