android-async-http 源码流程浅析

本文详细分析了android-async-http库的请求执行流程,从get、put等方法开始,通过sendRequest创建HttpUriRequest并提交到线程池。接着在AsyncHttpRequest的run方法中处理请求,包括预处理、执行、重试机制和结果回调。makeRequestWithRetries方法处理网络异常并决定是否重试。当请求被取消时,通过cancelRequests方法中断请求。整个流程涉及请求的生命周期管理和错误处理,对外提供灵活的回调接口。
摘要由CSDN通过智能技术生成
一、get()\put()\post()\delete()\head()\patch() 的执行流程分析

Step1    
个接口首先分装对应的HttpGet、HttpPut、HttpPost等,然后再封装成HttpUriRequest;
调用sendRequest(...):
/**
* Puts a new request in queue as a new thread in pool to be executed
*
* @param client HttpClient to be used for request, can differ in single requests
* @param contentType MIME body type, for POST and PUT requests, may be null
* @param context Context of Android application, to hold the reference of request
* @param httpContext HttpContext in which the request will be executed
* @param responseHandler ResponseHandler or its subclass to put the response into
* @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete,
* HttpPost, HttpGet, HttpPut, etc.
* @return RequestHandle of future request process
*/
protected RequestHandle sendRequest(DefaultHttpClient client,
HttpContext httpContext,
HttpUriRequest uriRequest,
String contentType,
ResponseHandlerInterface responseHandler,
Context context)
{
if (uriRequest == null) {
throw new IllegalArgumentException("HttpUriRequest must not be null");
}

if (responseHandler == null) {
throw new IllegalArgumentException("ResponseHandler must not be null");
}

if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
}

if (contentType != null) {
if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null && uriRequest.containsHeader(HEADER_CONTENT_TYPE)) {
log.w(LOG_TAG
, "Passed contentType will be ignored because HttpEntity sets content type");
} else {
uriRequest.setHeader(HEADER_CONTENT_TYPE
, contentType);
}
}

responseHandler.setRequestHeaders(uriRequest.getAllHeaders())
;
responseHandler.setRequestURI(uriRequest.getURI());

AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context);
threadPool.submit(request);
RequestHandle requestHandle = new RequestHandle(request);

if (context != null) {
List<RequestHandle> requestList
;
// Add request to request map
synchronized (requestMap) {
requestList = requestMap.get(context)
;
if (requestList == null) {
requestList = Collections.synchronizedList(
new LinkedList<RequestHandle>());
requestMap.put(context, requestList)
Asynchronous Http Client for Android Build Status An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries. Changelog See what is new in version 1.4.9 released on 19th September 2015 https://github.com/loopj/android-async-http/blob/1.4.9/CHANGELOG.md Javadoc Latest Javadoc for 1.4.9 release are available here (also included in Maven repository): https://loopj.com/android-async-http/doc/ Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happen outside the UI thread Requests use a threadpool to cap concurrent resource usage GET/POST params builder (RequestParams) Multipart file uploads with no additional third party libraries Tiny size overhead to your application, only 60kb for everything Automatic smart request retries optimized for spotty mobile connections Automatic gzip response decoding support for super-fast requests Optional built-in response parsing into JSON (JsonHttpResponseHandler) Optional persistent cookie store, saves cookies into your app's SharedPreferences Examples For inspiration and testing on device we've provided Sample Application. See individual samples here on Github To run Sample application, simply clone the repository and run this command, to install it on connected device gradle :sample:installDebug Maven You can now integrate this library in your project via Maven. There are available two kind of builds. releases, maven central https://repo1.maven.org/maven2/com/loopj/android/android-async-http/ Maven URL: https://repo1.maven.org/maven2/ GroupId: com.loopj.android ArtifactId: android-async-http Version: 1.4.9 Packaging: JAR or AAR Gradle repositories { maven { mavenCentral() } } dependencies { compile 'com.loopj.android:android-async-http:1.4.9' } development snapshots https://oss.sonatype.org/content/repositories/snapshots/com/loopj/android/android-async-http/ Maven URL: https://oss.sonatype.org/content/repositories/snapshots/ GroupId: com.loopj.android ArtifactId: android-async-http Version: 1.5.0-SNAPSHOT Packaging: JAR or AAR Gradle repositories { maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { compile 'com.loopj.android:android-async-http:1.5.0-SNAPSHOT' } Documentation, Features and Examples Full details and documentation can be found on the project page here: https://loopj.com/android-async-http/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值