Android網絡請求框架android-async-http


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中。




發佈了12 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章