Android-async-http Usage Records

Source: Internet
Author: User
Tags ustring

Introduced:

Android-async-http is an open source network request class library for Android. Official project Address: http://loopj.com/android-async-http/. Network request is simply to get data from the server, upload data to the server, the network request library is based on the Apache HttpClient Library, an asynchronous network request processing library, network processing is based on the Android non-UI thread, does not block the main UI thread, and the request result is processed by the callback method. Android-async-http processing normal access to content, downloading, uploading, including with cookies, is no problem, and the famous Instagram is the class library.

Use:

Download the dependent Jar package from the official website, import the Libs folder in the project and add it to the project path.

Let's take a look at the basic usage :

Asynchttpclient client = new Asynchttpclient (); Client.get ("http://www.google.com", new Asynchttpresponsehandler () {    @Override public    void OnStart () {        //called before request was started    }    @Override public    Void onsuccess (int statusCode, header[] headers, byte[] response) {        //Called when response HTTP status is "OK"    }
    @Override public    void onfailure (int statusCode, header[] headers, byte[] errorresponse, Throwable e) {        //CAL LED when response HTTP status was "4XX" (eg. 401, 403, 404)    }    @Override public    void onretry (int retryno) {
   //called when request is retried}});
As you can see, all network requests are implemented by Asynchttpclient instances.

directory structure of the class library

first look at the introduction of the main class:

    • Asynchttpclient: Asynchronous request client, class library for instance of network request
    • Asynchttprequest: Inherit Runnabler, submit to thread pool to execute network request and send start,success message
    • Asynchttpresponsehandler: Handles custom messages such as successful start of network request completion, etc.
    • Basejsonhttpresponsehandler: Inherited Texthttpresponsehandler, is a generic class that provides a parseresponse method that subclasses need to provide implementations that parse the request result into the desired type.
    • Binaryhttpresponsehandler: Inherit Asynchttpresponsehandler, byte stream returns processing the library for processing pictures, etc.
    • Jsonhttpresponsehandler: Inheriting Texthttpresponsehandler, also rewriting onsuccess and OnFailure methods, Convert the request result from string to Jsonobject or Jsonarray
    • Serializablecookie: Integrated Serializable for serializing cookies for access to cookies
    • Texthttpresponsehandler: Inherits Asynchttpresponsehandler, Overrides Asynchttpresponsehandler's onsuccess and OnFailure methods, Converts the request result from a byte array to a string

The official recommendation is to use a static asynchttpclient, the official example:

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;  }}
In use, we can directly call the request method through the class name, you can encapsulate a number of different request methods, such as parameters of different methods, download methods, upload methods and so on.

It's my own package. Some common methods:

public class Httputil {private static asynchttpclient httpclient = new Asynchttpclient (); Static{httpclient.settimeout ( 5000);} public static void Get (String Urlstring,asynchttpresponsehandler res)//Get a String Object {Httpclient.get (Urlstri) with a full URL    Ng, Res); public static void Get (String urlstring,requestparams params,asynchttpresponsehandler res)//url with parameter {HTTPC    Lient.get (urlstring, params,res); public static void Get (String urlstring,jsonhttpresponsehandler res)//without parameters, get JSON object or array {httpclient.get (URL    String, RES);    public static void Get (String urlstring,requestparams params,jsonhttpresponsehandler res)//with parameters, get JSON object or array {    Httpclient.get (urlstring, params,res); The public static void get (String ustring, Binaryhttpresponsehandler bhandler)//download data is used, which returns the byte data {httpclient.ge    T (ustring, Bhandler);     The public static void post (String ustring,requestparams params, Jsonhttpresponsehandler bhandler)//post data is used, which returns the JSON data  {  Httpclient.post (Ustring,params,bhandler);    public static void post (String ustring,requestparams params,asynchttpresponsehandler res)//post data used, return normal hand {    Httpclient.post (ustring, params,res);    } public static Asynchttpclient Getclient () {return httpclient; }}

Android-async-http Usage Records

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.