1.简介
Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnect,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息。
2.特性
(1)采用异步http请求,并通过匿名内部类处理回调结果(2)http请求独立在UI主线程之外
(3)采用线程池来处理并发请求
(4)采用RequestParams类创建GET/POST参数
(5)不需要第三方包即可支持Multipart file文件上传
(6)大小只有25kb
(7)自动为各种移动电话处理连接断开时请求重连
(8)超快的自动gzip响应解码支持
(9)使用BinaryHttpResponseHandler类下载二进制文件(如图片)
(10) 使用JsonHttpResponseHandler类可以自动将响应结果解析为json格式
(11)持久化cookie存储,可以将cookie保存到你的应用程序的SharedPreferences中
3.常用方法
<span style="font-size:14px;">public class HttpUtil {
private static AsyncHttpClient client =new AsyncHttpClient(); //实例话对象
static
{
client.setTimeout(11000); //设置链接超时,如果不设置,默认为10s
}
public static void get(String urlString,AsyncHttpResponseHandler res) //用一个完整url获取一个string对象
{
client.get(urlString, res);
}
public static void get(String urlString,RequestParams params,AsyncHttpResponseHandler res) //url里面带参数
{
client.get(urlString, params,res);
}
public static void get(String urlString,JsonHttpResponseHandler res) //不带参数,获取json对象或者数组
{
client.get(urlString, res);
}
public static void get(String urlString,RequestParams params,JsonHttpResponseHandler res) //带参数,获取json对象或者数组
{
client.get(urlString, params,res);
}
public static void get(String uString, BinaryHttpResponseHandler bHandler) //下载数据使用,会返回byte数据
{
client.get(uString, bHandler);
}
public static AsyncHttpClient getClient()
{
return client;
}
}</span>
4.RequestParams几种使用
(1)在RequestParams 对象中添加单个参数:
<span style="font-size:18px;"> RequestParams params = new RequestParams("single", "value");</span>
(2)在RequestParams 对象中添加单个参数:
<span style="font-size:18px;"> RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data");</span>
(3)在RequestParams 对象中添加InputStream用于上传:
<span style="font-size:18px;">InputStream myInputStream = blah;
RequestParams params = new RequestParams();
params.put("secret_passwords", myInputStream, "passwords.txt");</span>
(4)添加文件对象用于上传:
<span style="font-size:18px;">File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}</span>
(5)添加字节数组用于上传:
<span style="font-size:18px;"> byte[] myByteArray = blah;
RequestParams params = new RequestParams();
params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");</span>
(6)用BinaryHttpResponseHandler下载二进制数据:
<span style="font-size:18px;">AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
client.get("http://example.com/file.png", new BinaryHttpResponseHandler(allowedContentTypes) {
@Override
public void onSuccess(byte[] fileData) {
// Do something with the file
}
});</span>
5.对比volley优缺点
1.http请求独立在UI主线程之外,不阻塞UI线程,可以在返回数据回调方法中更新UI。
2.android-async-http可以更好的上传多个文件或大文件,两者都不支持断点续传,需要自己实现。
3.android-async-http可以持久化cookie存储,可以将cookie保存到你的应用程序的SharedPreferences中。