android android-async-http下载,android-async-http源码剖析

本文主要内容

android-async-http用法

android-async-http主要流程

总结

1、android-async-http用法

android-async-http框架用于网络请求等,据说它已经不再维护,但个人感觉它还是很好用的,尤其是源码中的一些设计思路,值得我们学习。

它的使用较为简单,如果是Android studio,则需要在build.gradle中添加如下语句:

implementation 'com.loopj.android:android-async-http:1.4.9'

如果是Eclipse,添加jar包就行了,jar包可以到github上下载。

它的使用非常简单,我们以最简单的get请求为例:

private AsyncHttpClient mClient = new AsyncHttpClient();

private String URL = "http://httpbin.org/get";

mClient.get(getActivity(), URL, handler);

private AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {

@Override

public void onSuccess(int statusCode, Header[] headers, byte[] response) {

if (response != null) {

Log.i("okunu", "onSuccess response = " + new String(response));

}else {

Log.i("okunu", "onSuccess");

}

}

@Override

public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable e) {

if (response != null) {

Log.i("okunu", "onFailure response = " + new String(response));

}else {

Log.i("okunu", "onFailure");

}

}

};

从代码上看,写相应的回调接口,并且将context以及url传入即可。需要注意的是,回调的接口有非常多的类型,需要根据服务器上返回的数据确定对应的类型,否则会出错。

android-async-http有两种类型的client,一种就是上文中用到的AsyncHttpClient ,另一种就是SyncHttpClient ,顾名思义,前一种是异步的,后一种是同步的,本文使用异步举例。

2、android-async-http主要流程

我们先来看看它的大致流程图:

a267859444d6

调用AsyncHttpClient 的get方法后,其实是将请求封装成一个 Runnable 对象,并且向线程池中提交这个任务。最后调用回调接口,将结果返回到主线程中来。

其实,关于android-async-http内部更多的核心流程,没有更多的可说了,代码又简单又复杂,上述的核心原理非常简单,但更细节的,比如网络访问,代码太细了,一入代码深似海,头大。所以本文不再详细记叙了,不过有一点非常重要的是,它的核心机制我们可以学习。

记得在工作中遇到过一个问题,使用系统自带的DownloadManager下载文件,有概率出错,下载的文件不全。这就非常郁闷了,系统的代码我也改不了,所以当时就自己弄了一套下载工具,到今天来看,没想到和 android-async-http 原理一模一样,根据请求生成一个任务,将任务提交到线程池中运行,可以在下载任务中抽象出进度接口,开始和结束接口。

如果我们不是做一个大而全的网络模块,如果只应对单一类型的网络访问或者类型有限的网络访问,完全可以模仿 android-async-http,自己写一个工具,清晰又简单。

总结

多看多想多总结,大道至简,很多原理其实都是相通的,我们可以借鉴,学以至用,这是最关键的。

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、付费专栏及课程。

余额充值