Android-async-http open source project introduction and usage

Source: Internet
Author: User

The android-async-http open-source project can be used to easily obtain network data or send data to servers. The introduction to the android-async-http open-source project comes from the official website: bytes

1.1Overview(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.

Translation:

The callback-based Http client is asynchronously built for Android and is based on the Apache HttpClient library. All requests are outside the application main thread UI, but any callback logic executes the callback on the same thread and uses the Android processing program to create message transmission.

1.2Features(Features)

  • Make asynchronous HTTP requests, handle responses in anonymous callbacks
  • Asynchronous HTTP request, response processing completed in anonymous callback
  • HTTP requests happen outside the UI thread
  • The HTTP request occurs outside the UI thread.
  • Requests use a threadpool to cap concurrent resource usage
  • The request uses threadpool to limit the usage of concurrent Resources
  • GET/POST params builder (RequestParams)
  • GET/POST parameter Construction and Use (RequestParams)
  • Multipart file uploads with no additional third party libraries
  • Multipart File Upload without additional third-party Libraries
  • Tiny size overhead to your application, only 25kb for everything
  • With a small overhead, you can do everything in just 25 kb.
  • Automatic smart request retries optimized for spotty mobile connections
  • Automatic Smart Request retry, optimized mobile connections of different quality
  • Automatic gzip response decoding support for super-fast requests
  • Automatic decoding supports extremely fast gzip response.
  • Binary file (images etc) downloadingBinaryHttpResponseHandler
  • Binary files (images, etc.) are downloaded using BinaryHttpResponseHandler
  • Built-in response parsing into JSONJsonHttpResponseHandler
  • The built-in response is parsed into JSON and JsonHttpResponseHandler is used
  • Persistent cookie store, saves cookies into your app's SharedPreferences
  • Persistent cookie storage: saves the cookie to your application's SharedPreferences.

    2.Installation & Basic Usage(Installation and basic usage)

    Download the latest. jar file from github and place it in your Android app'slibs/Folder.

    Download the latest jar file from github and place it in the libs/folder of your Android application.

    2.1 Download Method:

    1. Download from http://loopj.com/android-async-http/

    Click DownLoad to DownLoad the latest jar file.

    2. Download from https://github.com/loopj/android-async-http.


    Find DownLoad ZIP to DownLoad the file. The decompressed directory is as follows:


    Examples: A simple example is provided. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/uxL + release/release + z8LD5rXEzsS8/r + release + tcRqYXLOxLz + release/CvM/CvLS/release vcD4KPHA + release = "brush: java; "> import com.loopj.android.http.*;

    Create a newAsyncHttpClientInstance 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 ParametersRequestParams

    TheRequestParamsClass is used to add optional GET or POST parameters to your requests.RequestParamsCan be built and constructed in various ways:

    Create emptyRequestParamsAnd immediately add some parameters:

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

    CreateRequestParamsFor a single parameter:

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

    CreateRequestParamsFrom an existingMapOf key/value strings:

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

    See the RequestParams Javadoc for more information.

    AddInputStreamToRequestParamsTo upload:

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

    AddFileObject toRequestParamsTo 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 toRequestParamsTo 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 DataBinaryHttpResponseHandler

    TheBinaryHttpResponseHandlerClass 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.