volly的发送和接受请求实现类为HurlStack
实际发送请求的为HttpURLConnection
在此函数中执行@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError { }
。。。
HttpURLConnection connection = openConnection(parsedUrl, request);
。。。
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
设置http请求的header
setConnectionParametersForRequest(connection, request);
在此函数中设置http请求的content-type以及设置http body
默认request的构造函数为 Method.DEPRECATED_GET_OR_POST
public Request(String url, Response.ErrorListener listener) {
this(Method.DEPRECATED_GET_OR_POST, url, listener);
}
case Method.DEPRECATED_GET_OR_POST:
// This is the deprecated way that needs to be handled for backwards compatibility.
// If the request's post body is null, then the assumption is that the request is
// GET. Otherwise, it is assumed that the request is a POST.
byte[] postBody = request.getPostBody();
if (postBody !=null) {
// Prepare output. There is no need to set Content-Length explicitly,
// since this is handled by HttpURLConnection using the size of the prepared
// output stream.
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty(HEADER_CONTENT_TYPE,
request.getPostBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(postBody);
out.close();
}
break;
volly中如果没有进行修改 返回表单content-type
public String getBodyContentType() {
return"application/x-www-form-urlencoded; charset=" + getParamsEncoding();
}
post 或者 get的请求处理处理为
private staticvoid addBodyIfExists(HttpURLConnection connection, Request<?> request)
throws IOException, AuthFailureError {
byte[] body = request.getBody();
if (body !=null) {
connection.setDoOutput(true);
connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(body);
out.close();
}
}
在request类中
abstract protected Response<T> parseNetworkResponse(NetworkResponseresponse);
为必须要实现的抽象方法
此方法在CacheDispatcher 和 NetworkDispatcher方法中的run方法中被调用
// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
Response<?> response =request.parseNetworkResponse(
new NetworkResponse(entry.data,entry.responseHeaders));
request.addMarker("cache-hit-parsed”);
在NetworkDispatcher中,
NetworkResponse networkResponse =mNetwork.performRequest(request);
。。。
NetworkResponse networkResponse =mNetwork.performRequest(request); 在HurlStack中被定义,执行具体的网络请求。
在/**
* Subclasses must implement this to perform delivery of the parsed
* response to their listeners. The given response is guaranteed to
* be non-null; responses that fail to parse are not delivered.
* @param response The parsed response returned by
* {@link #parseNetworkResponse(NetworkResponse)}
*/
abstractprotected void deliverResponse(Tresponse);
if (mResponse.isSuccess()) {
mRequest.deliverResponse(mResponse.result);
} else {
mRequest.deliverError(mResponse.error);
}
调用回调
在此回调中mResponse mRequest都为final变量

本文详细介绍了Volley库中如何实现HTTP请求的发送与接收,包括使用HurlStack及HttpURLConnection进行网络请求的具体流程,同时深入探讨了POST与GET请求的处理方式。
529

被折叠的 条评论
为什么被折叠?



