Explanation of Andorid Async-HttpClient, asynchttpclient

Source: Internet
Author: User

Explanation of Andorid Async-HttpClient, asynchttpclient

Async-httpclient is an http access open-source framework for Android Application Development. It is open-source on GitHub. As it is not seen on GitHub today, I provide one here, it was downloaded from GitHub earlier. The version is V1.4.5. On GitHub, many developers still provide http access encapsulation classes, but I personally think Async-httpclient write is very good, so I will summarize the code structure and calling method in this open-source package.

The Async-httpclient open-source package provides http synchronous and asynchronous access, http get, post, set, delete, and other methods, and transmits different callback interfaces when calling the http access interface, the returned data is also encapsulated. After carefully reading the source code, it will be clear.

After unlocking the resource package ,? \ Library \ src \ main \ java \ com \ loopj \ android \ http this directory is the source code written by the author. There are only 27 classes. We recommend that you? Copy the com folder under the \ library \ src \ main \ java \ directory to the root directory of the Android project and set it to "use as source folder ", in this way, you can view the source code and write code while writing the call example.

The main classes in the source package are:

AsyncHttpClient. java asynchronous http listener class

SyncHttpClient. java synchronous http listener class

AsyncHttpRequest. java inherits the Runnable object and actually accesses the http thread.

ResponseHandlerInterface. javahttp access callback interface, which has many subclasses that encapsulate the returned data.

There are four main categories, including custom https certificates, http redirection, base64, cookie storage, and http access retries. These can be added when necessary.

While reading the source code, ResponseHandlerInterface has a large number of sub-classes and has experienced what is overload and overwriting! This is the idea of object-oriented programming. The following simple example shows that the code using this open-source package is concise and you can write less unnecessary duplicate code.

The subclass relationship of ResponseHandlerInterface is as follows:


In actual development, we recommend that you write an http access instance as a singleton.

public class MainActivity extends Activity {private static AsyncHttpClient mHttpClient = new AsyncHttpClient();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getData() ;}public void getData(){mHttpClient.get("http://www.baidu.com", responseHandler) ;}AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {@Overridepublic void onSuccess(int statusCode, Header[] headers,byte[] responseBody) {for(Header header :headers){Log.e("onSuccess","" + header.getName() + " = " + header.getValue()) ;}Log.e("onSuccess","statusCode = " + statusCode + ", responseBody = " + new String(responseBody)) ;}@Overridepublic void onFailure(int statusCode, Header[] headers,byte[] responseBody, Throwable error) {for(Header header :headers){Log.e("onFailure","" + header.getName() + " = " + header.getValue()) ;}Log.e("onFailure","statusCode = " + statusCode + " ,responseBody = " + responseBody) ;}} ;}



In the android-async-http framework, how does one set the User-Agent parameter and AsyncHttpClient?

It is not recommended that you use async-http for simple reasons, because the initiate request and update interface are activity or service operations. These operations, the code of each interface is basically different and cannot be universal.
We recommend that you use the MVC development mode to encapsulate an http tool class, which only transmits one URL, Map, and character encoding.

Encapsulate several methods, such as initiating a get or post request, and return the results returned by the server to the call site

Let the call process parse the data and handle exceptions, and then return the results to a page ..



Async-http-client is easy to use

Easy to learn
 

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.