08_android入門_android-async-http開源項目介紹及使用方法

來源:互聯網
上載者:User

android-async-http開源項目可以是我們輕鬆的擷取網路資料或者向伺服器發送資料,使用起來非常簡單,關於android-async-http開源項目的介紹內容來自於官方:http://loopj.com/android-async-http/.下面我對此首頁上內容進行大體上的翻譯,希望能夠對你理解android-async-http開源項目有所協助

1.1 Overview(概況)

An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing.

譯文:

非同步基於回調的Http用戶端為Android構建,是基於Apache HttpClient庫的。所有的請求都是位於應用程式主線程 UI 之外,但任何回調邏輯將相同的線程上執行回調,使用Android的處理常式建立訊息傳遞。

1.2 Features(特徵)

  • Make asynchronous HTTP requests, handle responses in anonymous callbacks
  • 進行非同步HTTP請求,處理響應在匿名回調中完成
  • HTTP requests happen outside the UI thread
  • HTTP請求發生在UI線程之外
  • Requests use a threadpool to cap concurrent resource usage
  • 請求使用threadpool,限制並發資源使用方式
  • GET/POST params builder (RequestParams)
  • GET / POST參數構建使用(RequestParams)
  • Multipart file uploads with no additional third party libraries
  • Multipart 檔案上傳,沒有額外的第三方庫
  • Tiny size overhead to your application, only 25kb for everything
  • 在你的應用程式上利用很小的開銷,僅僅25 kb就可以做一切
  • Automatic smart request retries optimized for spotty mobile connections
  • 自動智能請求重試,最佳化了品質不一的移動串連
  • Automatic gzip response decoding support for super-fast requests
  • 自動解碼支援gzip反應速度超快的請求
  • Binary file (images etc) downloading with BinaryHttpResponseHandler
  • 二進位檔案(圖片等)的下載,使用BinaryHttpResponseHandler
  • Built-in response parsing into JSON with JsonHttpResponseHandler
  • 內建響應解析成JSON,使用JsonHttpResponseHandler
  • Persistent cookie store, saves cookies into your app’s SharedPreferences
  • 持久化cookie儲存,儲存cookie到你的應用程式的SharedPreferences

    2.Installation & Basic Usage(安裝和基本用法)

    Download the latest .jar file from github and place it in your Android app’s libs/ folder.

    從github上下載最新的最新的jar檔案.並將其放置在你的Android應用程式的libs /檔案夾.

    2.1下載方式:

    1.從http://loopj.com/android-async-http/的頁面下載

    點擊DownLoad即可下載最新的jar檔案

    2.從https://github.com/loZ喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcGovYW5kcm9pZC1hc3luYy1odHRwtcTSs8Pmz8LU2DwvcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140526/2014052609121926.jpg" alt="\">

    找到DownLoad ZIP進行下載檔案,解壓後的目錄如下


    examples:裡面有簡單的例子

    library:裡面存放的是android-async-http開源項目的源碼(方法一:可以把library\src\main\java檔案下面的檔案拷貝到,你應用的src下也可以直接使用)

    releases:裡面存放的是各個版本的jar檔案,(方法二:只需把最新的jar檔案拷貝到你應用的libs目錄下即可.)

    samples:裡面存放的也是例子(可供參考)

    備忘:方法一和方法二隻能採用其中之一,建議採用方法二

    2.2使用方法

    Import the http package.

    import com.loopj.android.http.*;

    Create a new AsyncHttpClient instance and make a request:

    AsyncHttpClient client = new AsyncHttpClient();client.get("http://www.google.com", new AsyncHttpResponseHandler() {    @Override    public void onSuccess(String response) {        System.out.println(response);    }});

    Adding GET/POST Parameters with RequestParams

    The RequestParams class is used to add optional GET or POST parameters to your requests.RequestParams can be built and constructed in various ways:

    Create empty RequestParams and immediately add some parameters:

    RequestParams params = new RequestParams();params.put("key", "value");params.put("more", "data");

    Create RequestParams for a single parameter:

    RequestParams params = new RequestParams("single", "value");

    Create RequestParams from an existing Map of key/value strings:

    HashMap paramMap = new HashMap();paramMap.put("key", "value");RequestParams params = new RequestParams(paramMap);

    See the RequestParams Javadoc for more information.

    Add an InputStream to the RequestParams to upload:

    InputStream myInputStream = blah;RequestParams params = new RequestParams();params.put("secret_passwords", myInputStream, "passwords.txt");

    Add a File object to the RequestParams to upload:

    File myFile = new File("/path/to/file.png");RequestParams params = new RequestParams();try {    params.put("profile_picture", myFile);} catch(FileNotFoundException e) {}

    Add a byte array to the RequestParams to upload:

    byte[] myByteArray = blah;RequestParams params = new RequestParams();params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");

    See the RequestParams Javadoc for more information.

    Downloading Binary Data with BinaryHttpResponseHandler

    The BinaryHttpResponseHandler class can be used to fetch binary data such as images and other files. For example:

    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    }});

    See the BinaryHttpResponseHandler Javadoc for more information.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.