Android async-http(異步 HTTP 客戶端開發包)

jopen 11年前發布 | 41K 次閱讀 Android Android開發 移動開發

AsyncHttpClient 這個類庫可以在Java應用程序中執行HTTP 請求并異步處理HTTP響應。用法非常簡單。

項目如圖:

20130503215223861.jpeg

源碼分析:

public class AsyncHttpResponseHandler {  //請求返回處理  成功 失敗 開始  完成  等自定義的消息

public class BinaryHttpResponseHandler extends AsyncHttpResponseHandler { //字節流返回處理 該庫用于處理圖片等

public class JsonHttpResponseHandler extends AsyncHttpResponseHandler { //json請求返回處理  服務器與客戶端用json交流時使用

class AsyncHttpRequest implements Runnable { //基于線程 異步請求 通過AsyncHttpResponseHandler回調

public class PersistentCookieStore implements CookieStore { //HttpClient處理數據 使用cookie持久性存儲接口

public class RequestParams { //封裝了參數處理  例如:
    * RequestParams params = new RequestParams();

* params.put("username", "james");  
* params.put("password", "123456");  
* params.put("email", "my@email.com");  
* 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  
*  
* AsyncHttpClient client = new AsyncHttpClient();  
* client.post("http://myendpoint.com", params, responseHandler);  </pre></span></span>class RetryHandler implements HttpRequestRetryHandler {//多個線程同步處理 <p><span style="line-height:21px;font-family:微軟雅黑,Verdana,sans-serif,宋體;font-size:12px;">public class SerializableCookie implements Serializable { //操作cookie 放入/取出數據</span></p>

class SimpleMultipartEntity implements HttpEntity { //處理多個請求實體封裝

public abstract class SyncHttpClient extends AsyncHttpClient { //同步客戶端請求

public class AsyncHttpClient { //異步客戶端請求 如:
    * AsyncHttpClient client = new AsyncHttpClient();

* client.get("http://www.google.com", new AsyncHttpResponseHandler() {  
*   @Override  
*   public void onSuccess(String response) {  
*       System.out.println(response);  
*   }  
* });  </pre></span><br />

操作實例如:

    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;  
}  </pre>下載:<img title="zip.png" src="https://simg.open-open.com/show/42a8c3e06b0df6f814686984be88c6b4.png" /> <a title="android-async-http-master.zip" href="/misc/goto?guid=4959545658430025446">android-async-http-master.zip</a>
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!