JAR——android-async-http-1.4.5

來源:互聯網
上載者:User

標籤:網路   private   public   

  1. http架構:實現非同步訪問網路;

  2. 使用方法:

(1)建立HttpUtil

public class HttpUtil {private static AsyncHttpClient client = new AsyncHttpClient(); // 執行個體話對象static {client.setTimeout(11000); // 設定連結逾時,如果不設定,預設為10s}/** * url不帶參數返回String * @param urlString url * @param res 處理handler */public static void get(String urlString, AsyncHttpResponseHandler res){ client.get(urlString, res);}/** * url帶參數返回String * @param urlString url * @param params url中的參數 * @param res 處理handler */public static void get(String urlString, RequestParams params,AsyncHttpResponseHandler res){ // url裡面帶參數client.get(urlString, params, res);}/** * 不帶參數返回json * @param urlString url  * @param res 處理handler */public static void get(String urlString, JsonHttpResponseHandler res){ // 不帶參數,擷取json對象或者數組client.get(urlString, res);}/** * 帶參數,返回json * @param urlString url * @param params url參數 * @param res 處理handler */public static void get(String urlString, RequestParams params,JsonHttpResponseHandler res){ // 帶參數,擷取json對象或者數組client.get(urlString, params, res);}/** * 不帶參數,返回byte資料 * @param uString url * @param bHandler 處理handler */public static void get(String uString, BinaryHttpResponseHandler bHandler){ // 下載資料使用,會返回byte資料client.get(uString, bHandler);}public static AsyncHttpClient getClient() {return client;}}

(2)在Activity中使用

//url擷取String url = getString(R.string.url);//初始化網路請求參數RequestParams params = new RequestParams();params.put(username, username);params.put(password, password);//網路請求,處理返回資料HttpUtil.get(MyConsts.URL_LOGIN, params, new JsonHttpResponseHandler(){//網路請求成功public void onSuccess(int statusCode, Header[] headers,JSONObject response) {//成功擷取網路資料,在這裡處理網路返回的資料}//伺服器請求失敗public void onFailure(int statusCode, Header[] headers,String responseString, Throwable throwable) {                                    //訪網路失敗}//請求結束,不管成功與否都調用該方法public void onFinish() {                                        //無論成功與否都會執行該方法}});

特別注意:onFinish方法在onSucess()和onFailure()方法執行之前執行;

JAR——android-async-http-1.4.5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.