1. 程式人生 > >Android Asynchronous Http Client-Android非同步網路請求客戶端介面

Android Asynchronous Http Client-Android非同步網路請求客戶端介面

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.使用方法

(1)到官網http://loopj.com/android-async-http/下載最新的android-async-http-1.4.4.jar,然後將此jar包新增進Android應用程式 libs資料夾
(2)通過import com.loopj.android.http.*;引入相關類
(3)建立非同步請求

[java] view plaincopyprint?
  1. AsyncHttpClient client = new AsyncHttpClient();  
  2. client.get("http://www.google.com"new AsyncHttpResponseHandler() {  
  3.     @Override
  4.     publicvoid onSuccess(String response) {  
  5.         System.out.println(response);  
  6.     }  
  7. });  
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        System.out.println(response);
    }
});

4.建議使用靜態的Http Client物件
在下面這個例子,我們建立了靜態的http client物件,使其很容易連線到Twitter的API
[java] view plaincopyprint?
  1. import com.loopj.android.http.*;  
  2. publicclass TwitterRestClient {  
  3.   privatestaticfinal String BASE_URL = "http://api.twitter.com/1/";  
  4.   privatestatic AsyncHttpClient client = new AsyncHttpClient();  
  5.   publicstaticvoid get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {  
  6.       client.get(getAbsoluteUrl(url), params, responseHandler);  
  7.   }  
  8.   publicstaticvoid post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {  
  9.       client.post(getAbsoluteUrl(url), params, responseHandler);  
  10.   }  
  11.   privatestatic String getAbsoluteUrl(String relativeUrl) {  
  12.       return BASE_URL + relativeUrl;  
  13.   }  
  14. }  
import com.loopj.android.http.*;

public class TwitterRestClient {
  private static final String BASE_URL = "http://api.twitter.com/1/";

  private static AsyncHttpClient client = new AsyncHttpClient();

  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.get(getAbsoluteUrl(url), params, responseHandler);
  }

  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.post(getAbsoluteUrl(url), params, responseHandler);
  }

  private static String getAbsoluteUrl(String relativeUrl) {
      return BASE_URL + relativeUrl;
  }
}
然後我們可以很容易的在程式碼中操作Twitter的API
[java] view plaincopyprint?
  1. import org.json.*;  
  2. import com.loopj.android.http.*;  
  3. class TwitterRestClientUsage {  
  4.     publicvoid getPublicTimeline() throws JSONException {  
  5.         TwitterRestClient.get("statuses/public_timeline.json"nullnew JsonHttpResponseHandler() {  
  6.             @Override
  7.             publicvoid onSuccess(JSONArray timeline) {  
  8.                 // Pull out the first event on the public timeline
  9.                 JSONObject firstEvent = timeline.get(0);  
  10.                 String tweetText = firstEvent.getString("text");  
  11.                 // Do something with the response
  12.                 System.out.println(tweetText);  
  13.             }  
  14.         });  
  15.     }  
  16. }  
import org.json.*;
import com.loopj.android.http.*;

class TwitterRestClientUsage {
    public void getPublicTimeline() throws JSONException {
        TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONArray timeline) {
                // Pull out the first event on the public timeline
                JSONObject firstEvent = timeline.get(0);
                String tweetText = firstEvent.getString("text");

                // Do something with the response
                System.out.println(tweetText);
            }
        });
    }
}

5. AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三個類使用方法

(1)AsyncHttpClient

public class AsyncHttpClient extends java.lang.Object
 該類通常用在android應用程式中建立非同步GET, POST, PUT和DELETE HTTP請求,請求引數通過RequestParams例項建立,響應通過重寫匿名內部類 ResponseHandlerInterface的方法處理。
例子:
[java] view plaincopyprint?
  1. AsyncHttpClient client = new AsyncHttpClient();  
  2.  client.get("http://www.google.com"new ResponseHandlerInterface() {  
  3.      @Override
  4.      publicvoid onSuccess(String response) {  
  5.          System.out.println(response);  
  6.      }  
  7.  });  
AsyncHttpClient client = new AsyncHttpClient();
 client.get("http://www.google.com", new ResponseHandlerInterface() {
     @Override
     public void onSuccess(String response) {
         System.out.println(response);
     }
 });
(2)RequestParams
public class RequestParams extends java.lang.Object 
用於建立AsyncHttpClient例項中的請求引數(包括字串或者檔案)的集合
例子:
[java] view plaincopyprint?
  1. RequestParams params = new RequestParams();  
  2.  params.put("username""james");  
  3.  params.put("password""123456");  
  4.  params.put("email""[email protected]");  
  5.  params.put("profile_picture"new File("pic.jpg")); // Upload a File
  6.  params.put("profile_picture2", someInputStream); // Upload an InputStream
  7.  params.put("profile_picture3"new ByteArrayInputStream(someBytes)); // Upload some bytes
  8.  Map<String, String> map = new HashMap<String, String>();  
  9.  map.put("first_name""James");  
  10.  map.put("last_name""Smith");  
  11.  params.put("user", map); // url params: "user[first_name]=James&user[last_name]=Smith"
  12.  Set<String> set = new HashSet<String>(); // unordered collection
  13.  set.add("music");  
  14.  set.add("art");  
  15.  params.put("like", set); // url params: "like=music&like=art"
  16.  List<String> list = new ArrayList<String>(); // Ordered collection
  17.  list.add("Java");  
  18.  list.add("C");  
  19.  params.put("languages", list); // url params: "languages[]=Java&languages[]=C"
  20.  String[] colors = { "blue""yellow" }; // Ordered collection
  21.  params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"
  22.  List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();  
  23.  Map<String, String> user1 = new HashMap<String, String>();  
  24.  user1.put("age""30");  
  25.  user1.put("gender""male");  
  26.  Map<String, String> user2 = new HashMap<String, String>();  
  27.  user2.put("age""25");  
  28.  user2.put("gender""female");  
  29.  listOfMaps.add(user1);  
  30.  listOfMaps.add(user2);  
  31.  params.put("users", listOfMaps); // url params: "users[][age]=30&users[][gender]=male&users[][age]=25&users[][gender]=female"
  32.  AsyncHttpClient client = new AsyncHttpClient();  
  33.  client.post("http://myendpoint.com", params, responseHandler);  
RequestParams params = new RequestParams();
 params.put("username", "james");
 params.put("password", "123456");
 params.put("email", "[email protected]");
 params.put("profile_picture", new File("pic.jpg")); // Upload a File
 params.put("profile_picture2", someInputStream); // Upload an InputStream
 params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes

 Map<String, String> map = new HashMap<String, String>();
 map.put("first_name", "James");
 map.put("last_name", "Smith");
 params.put("user", map); // url params: "user[first_name]=James&user[last_name]=Smith"

 Set<String> set = new HashSet<String>(); // unordered collection
 set.add("music");
 set.add("art");
 params.put("like", set); // url params: "like=music&like=art"

 List<String> list = new ArrayList<String>(); // Ordered collection
 list.add("Java");
 list.add("C");
 params.put("languages", list); // url params: "languages[]=Java&languages[]=C"

 String[] colors = { "blue", "yellow" }; // Ordered collection
 params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"

 List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
 Map<String, String> user1 = new HashMap<String, String>();
 user1.put("age", "30");
 user1.put("gender", "male");
 Map<String, String> user2 = new HashMap<String, String>();
 user2.put("age", "25");
 user2.put("gender", "female");
 listOfMaps.add(user1);
 listOfMaps.add(user2);
 params.put("users", listOfMaps); // url params: "users[][age]=30&users[][gender]=male&users[][age]=25&users[][gender]=female"

 AsyncHttpClient client = new AsyncHttpClient();
 client.post("http://myendpoint.com", params, responseHandler);
(3)public class AsyncHttpResponseHandler extends java.lang.Object implements ResponseHandlerInterface
用於攔截和處理由AsyncHttpClient建立的請求。在匿名類AsyncHttpResponseHandler中的重寫 onSuccess(int, org.apache.http.Header[], byte[])方法用於處理響應成功的請求。此外,你也可以重寫 onFailure(int, org.apache.http.Header[], byte[], Throwable), onStart(), onFinish(), onRetry() 和onProgress(int, int)方法
例子:
[java] view plaincopyprint?
  1. AsyncHttpClient client = new AsyncHttpClient();  
  2.  client.get("http://www.google.com"new AsyncHttpResponseHandler() {  
  3.      @Override
  4.      publicvoid onStart() {  
  5.          // Initiated the request
  6.      }  
  7.      @Override
  8.      publicvoid onSuccess(int statusCode, Header[] headers, byte[] responseBody) {  
  9.          // Successfully got a response
  10.      }  
  11.      @Override
  12.      publicvoid onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)  
  13.  {  
  14.          // Response failed :(
  15.      }  
  16.      @Override
  17.      publicvoid onRetry() {  
  18.          // Request was retried
  19.      }  
  20.      @Override
  21.      publicvoid onProgress(int bytesWritten, int totalSize) {  
  22.          // Progress notification
  23.      }  
  24.      @Override
  25.      publicvoid onFinish() {  
  26.          // Completed the request (either success or failure)
  27.      }  
  28.  });  
AsyncHttpClient client = new AsyncHttpClient();
 client.get("http://www.google.com", new AsyncHttpResponseHandler() {
     @Override
     public void onStart() {
         // Initiated the request
     }

     @Override
     public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
         // Successfully got a response
     }

     @Override
     public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
 {
         // Response failed :(
     }

     @Override
     public void onRetry() {
         // Request was retried
     }

     @Override
     public void onProgress(int bytesWritten, int totalSize) {
         // Progress notification
     }

     @Override
     public void onFinish() {
         // Completed the request (either success or failure)
     }
 });

6.利用PersistentCookieStore持久化儲存cookie
PersistentCookieStore類用於實現Apache HttpClient的CookieStore介面,可以自動的將cookie儲存到Android裝置的SharedPreferences中,如果你打算使用cookie來管理驗證會話,這個非常有用,因為使用者可以保持登入狀態,不管關閉還是重新開啟你的app
(1)首先建立 AsyncHttpClient例項物件
[java] view plaincopyprint?
  1. AsyncHttpClient myClient = new AsyncHttpClient();  
AsyncHttpClient myClient = new AsyncHttpClient();
(2)將客戶端的cookie儲存到PersistentCookieStore例項物件,帶有activity或者應用程式context的構造方法
[java] view plaincopyprint?
  1. PersistentCookieStore myCookieStore = new PersistentCookieStore(this);  
  2. myClient.setCookieStore(myCookieStore);  
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
(3)任何從伺服器端獲取的cookie都會持久化儲存到myCookieStore中,新增一個cookie到儲存中,只需要構造一個新的cookie物件,並且呼叫addCookie方法
[java] view plaincopyprint?
  1. BasicClientCookie newCookie = new BasicClientCookie("cookiesare""awesome");  
  2. newCookie.setVersion(1);  
  3. newCookie.setDomain("mydomain.com");  
  4. newCookie.setPath("/");  
  5. myCookieStore.addCookie(newCookie);  
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);

7.利用RequestParams上傳檔案
類RequestParams支援multipart file 檔案上傳
(1)在RequestParams 物件中新增InputStream用於上傳
[java] view plaincopyprint?
  1. InputStream myInputStream = blah;  
  2. RequestParams params = new RequestParams();  
  3. params.put("secret_passwords", myInputStream, "passwords.txt");  
InputStream myInputStream = blah;
RequestParams params = new RequestParams();
params.put("secret_passwords", myInputStream, "passwords.txt");
(2)新增檔案物件用於上傳
[java] view plaincopyprint?
  1. File myFile = new File("/path/to/file.png");  
  2. RequestParams params = new RequestParams();  
  3. try {  
  4.     params.put("profile_picture", myFile);  
  5. catch(FileNotFoundException e) {}  
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
    params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
(3)新增位元組陣列用於上傳
[java] view plaincopyprint?
  1. byte[] myByteArray = blah;  
  2. RequestParams params = new RequestParams();  
  3. params.put("soundtrack"new ByteArrayInputStream(myByteArray), "she-wolf.mp3");  
byte[] myByteArray = blah;
RequestParams params = new RequestParams();
params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");

8.用BinaryHttpResponseHandler下載二進位制資料
[java] view plaincopyprint?
  1. BinaryHttpResponseHandler用於獲取二進位制資料如圖片和其他檔案  
  2. AsyncHttpClient client = new AsyncHttpClient();  
  3. String[] allowedContentTypes = new String[] { "image/png""image/jpeg" };  
  4. client.get("http://example.com/file.png"new BinaryHttpResponseHandler(allowedContentTypes) {  
  5.     @Override
  6.     publicvoid onSuccess(byte[] fileData) {  
  7.         // Do something with the file
  8.     }  
  9. });  
BinaryHttpResponseHandler用於獲取二進位制資料如圖片和其他檔案
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
    }
});