Volly 发送网络请求过程分析
来源:互联网 发布:landesk 软件中心 编辑:程序博客网 时间:2024/05/20 06:27
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)}
*/
abstractprotectedvoid deliverResponse(Tresponse);
if (mResponse.isSuccess()) {
mRequest.deliverResponse(mResponse.result);
} else {
mRequest.deliverError(mResponse.error);
}
调用回调
在此回调中mResponse mRequest都为final变量
- Volly 发送网络请求过程分析
- [转]Volly网络请求
- volly post请求
- 网络发送和接收过程分析
- Webkit页面加载过程分析(1)-发送请求篇
- 网络请求---NSURLConnection发送请求
- HttpClient发送网络请求
- jQuery发送网络请求
- java 发送网络请求
- android 网络通信框架volly
- 关于Volly框架请求中文乱码
- javaweb用户发送请求过程
- 发送http请求的过程
- 网络请求过程
- HttpClient 发送网络请求步骤
- 用NSURLSession发送网络请求
- 使用Session发送网络请求
- ios-异步发送网络请求
- matplotlib绘图基础
- char*,const char*和string 三者转换
- Spring+SpringMVC+MyBaties学习笔记(二)集成Spring SpringMVC MyBatis
- Web 研发模式演变 (转自玉伯)
- MFC添加图片后,编译报error RC2108:expected numerical dialog constant
- Volly 发送网络请求过程分析
- Displaying Graphics with OpenGL ES(四)——Applying Projection and Camera Views
- innodb_flush_method理解
- 这是一个开始
- 作为一个J2EE软件攻城师
- mysql安装和配置
- Android Stuidio解决更新下载时无法连接URL的问题
- 158 js timer函数
- webStorm 开启NodeJs Core library 使用