Android-Async-Http usage record

Source: Internet
Author: User
Tags ustring

Android-Async-Http usage record
Introduction:

Android-Async-Http is an open-source network request class library for Android. Official Project address: http://loopj.com/android-async-http /. Network requests are nothing more than retrieving data from the server and uploading data to the server. This network request library is an Asynchronous Network request processing Library Based on the Apache HttpClient library, network processing is based on non-UI threads of Android. It does not block the main UI thread and processes the request results through callback. Android-Async-Http handles normal access to content, downloads, uploads, and cookies. This class library is used by the famous Instagram.

Usage:

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 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}});
We can see that all network requests are implemented through the AsyncHttpClient instance.

Directory structure of the Class Library

Let's take a look at the introduction of the main classes: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + vH87/Nu6e2y6OswOC/4r340NDN + MLnx + vH87XEyrXA/Signature + signature + 8 + signature/Signature + signature + wp2juw.vcd4kpha + 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 ;}}When using this method, we can directly call the required request method through the class name. You can encapsulate different request methods, such as methods with different parameters, download methods, and upload methods.

Here are some of my encapsulated common methods:

Public class HttpUtil {private static AsyncHttpClient httpclient = new AsyncHttpClient (); static {httpclient. setTimeout (5000);} public static void get (String urlString, AsyncHttpResponseHandler res) // obtain a string object {httpclient. get (urlString, res);} public static void get (String urlString, RequestParams params, AsyncHttpResponseHandler res) // The url contains the parameter {httpclient. get (urlString, params, res);} public static void get (String urlString, JsonHttpResponseHandler res) // retrieves a json object or array {httpclient without parameters. get (urlString, res);} public static void get (String urlString, RequestParams params, JsonHttpResponseHandler) // obtain the json object or array {httpclient. get (urlString, params, res);} public static void get (String uString, BinaryHttpResponseHandler bHandler) // when downloading data, byte data {httpclient is returned. get (uString, bHandler);} public static void post (String uString, RequestParams params, JsonHttpResponseHandler bHandler) // when using post data, the returned json data {httpclient. post (uString, params, bHandler);} public static void post (String uString, RequestParams params, AsyncHttpResponseHandler res) // use the post data and return the normal user {httpclient. post (uString, params, res);} public static AsyncHttpClient getClient () {return httpclient ;}}

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.