Android-Async-Http使用記錄
介紹:
Android-Async-Http是一個Android的開源網路請求類庫。官方項目地址:http://loopj.com/android-async-http/。網路請求無非就是從伺服器上擷取資料,往伺服器上上傳資料,這個網路請求庫是基於Apache HttpClient庫之上的一個非同步網路請求處理庫,網路處理均基於Android的非UI線程,不會阻塞主UI線程,並且通過回調方法處理請求結果。Android-Async-Http處理正常的擷取內容,下載,上傳,包括帶cookies訪問都是沒有問題的,大名鼎鼎的Instagram就是用的這個類庫。
使用:
從官網下載依賴jar包,匯入工程中libs檔案夾下並添加到工程路徑即可。
先看一下基本用法:
AsyncHttpClient client = new AsyncHttpClient();client.get("http://www.google.com", new AsyncHttpResponseHandler() { @Override public void onStart() { // called before request is started } @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) { // called when response HTTP status is "200 OK" } @Override public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) { // called when response HTTP status is "4XX" (eg. 401, 403, 404) } @Override public void onRetry(int retryNo) { // called when request is retried}});
可以看出,所有的網路請求都是通過AsyncHttpClient執行個體來實現的。
類庫的目錄結構
先看下主要的類的介紹:<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgoKQXN5bmNIdHRwQ2xpZW50o7rS7LK9x+vH87/Nu6e2y6OswOC/4r340NDN+MLnx+vH87XEyrXA/UFzeW5jSHR0cFJlcXVlc3SjurzMs9BSdW5uYWJsZXKjrLG7c3VibWl01sHP37PMs9jWtNDQzfjC58frx/Oyoreiy81zdGFydKOsc3VjY2Vzc7XIz/vPokFzeW5jSHR0cFJlc3BvbnNlSGFuZGxlcqO6tKbA7c34wufH68fzs8m5piDKp7DcIL+qyrwgIM3qs8kgILXI19S2qNLltcTP+8+iQmFzZUpzb25IdHRwUmVzcG9uc2VIYW5kbGVyo7q8zLPQVGV4dEh0dHBSZXNwb25zZUhhbmRsZXKjrMrH0ru49re60M3A4KOszOG5qcHLcGFyc2VSZXNwb25zZbe9t6ijrNfTwODQ6NKqzOG5qcq1z9ajrL2rx+vH873hufu94s72s8nQ6NKqtcTA4NDNoaNCaW5hcnlIdHRwUmVzcG9uc2VIYW5kbGVyIKO6vMyz0CBBc3luY0h0dHBSZXNwb25zZUhhbmRsZXIgo6zX1r3awfe3tbvYtKbA7SC4w7/i08PT2rSmwO3NvMastchKc29uSHR0cFJlc3BvbnNlSGFuZGxlcqO6vMyz0FRleHRIdHRwUmVzcG9uc2VIYW5kbGVyo6zNrNH5ysfW2NC0b25TdWNjZXNzus1vbkZhaWx1cmW3vbeoo6y9q8frx/O94bn708lTdHJpbmfXqru7zqpKU09OT2JqZWN0u/JKU09OQXJyYXlTZXJpYWxpemFibGVDb29raWWjuryvs8kgU2VyaWFsaXphYmxlo6zTw9Pa0PLB0LuvY29va2llo6zS1LHjtObIoWNvb2tpZVRleHRIdHRwUmVzcG9uc2VIYW5kbGVyo7q8zLPQQXN5bmNIdHRwUmVzcG9uc2VIYW5kbGVyo6zW2NC0wctBc3luY0h0dHBSZXNwb25zZUhhbmRsZXK1xG9uU3VjY2Vzc7rNb25GYWlsdXJlt723qKOsvavH68fzveG5+9PJYnl0Zcr91+nXqru7zqpTdHJpbmcKPHA+PC9wPgo8cD4gICAgudm3vb2o0unKudPD0ru49r6yzKy1xEFzeW5jSHR0cENsaWVudKOsudm3vcq+wP2jujwvcD4KPHA+ICAgICA8L3A+CjxwcmUgY2xhc3M9"brush:java;">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; }} 在用的時候,我們可以直接通過類名調用需要的要求方法,你可以多封裝一些不同的要求方法,比如參數不同的方法,下載方法,上傳方法等。
這是我自己的封裝好的一些常用的方法:
public class HttpUtil {private static AsyncHttpClient httpclient = new AsyncHttpClient();static{httpclient.setTimeout(5000);}public static void get(String urlString,AsyncHttpResponseHandler res) //用一個完整url擷取一個string對象 {httpclient.get(urlString, res); } public static void get(String urlString,RequestParams params,AsyncHttpResponseHandler res) //url裡面帶參數 { httpclient.get(urlString, params,res); } public static void get(String urlString,JsonHttpResponseHandler res) //不帶參數,擷取json對象或者數組 { httpclient.get(urlString, res); } public static void get(String urlString,RequestParams params,JsonHttpResponseHandler res) //帶參數,擷取json對象或者數組 { httpclient.get(urlString, params,res); } public static void get(String uString, BinaryHttpResponseHandler bHandler) //下載資料使用,會返回byte資料 { httpclient.get(uString, bHandler); } public static void post(String uString,RequestParams params, JsonHttpResponseHandler bHandler) //post資料使用,會返回json資料 { httpclient.post(uString,params,bHandler); } public static void post(String uString,RequestParams params,AsyncHttpResponseHandler res) //post資料使用,返回普通的手 { httpclient.post(uString, params,res); } public static AsyncHttpClient getClient() { return httpclient; }}