Android Network request library-android-async-http

Source: Internet
Author: User

There is a well-known ASIHttpRequest library in iOS development that is used to process network request operations. Today we will introduce an Android-async-http network request library that is equally powerful on android, currently, Instagram and Pinterest Android are very popular apps that use this network request library. 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 and request results are processed through callback. Its main features are as follows: It processes asynchronous Http requests and processes callback results through anonymous internal classes. All Http requests are in non-UI threads, UI operations are not blocked. When the thread pool is used to process concurrent requests to process file uploads, download response results are automatically packaged, JSON format is used to automatically handle connection disconnections, And the request re-connection using android-async-http is also very simple, go to the official website. Use the following code to create an asynchronous request: [java] AsyncHttpClient client = new AsyncHttpClient (); client. get ("http://www.baidu.com", new AsyncHttpResponseHandler () {@ Override public void onSuccess (String response) {System. out. println (response); textView. setText (response) ;}@ Override public void onStart () {super. onStart (); System. out. println ("onStart") ;}@ Override public void onFinish () {super. onFinish (); System. out. pr Intln ("onFinish") ;}} use the URL specified by the Get request and the callback function to process the request results. At the same time, the request methods also support POST and PUT, the request also supports parameter transmission. The following describes how to use a JSON string as a parameter to access the server: [java] try {JSONObject jsonObject = new JSONObject (); jsonObject. put ("username", "ryantang"); StringEntity stringEntity = new StringEntity (jsonObject. toString (); client. post (MainActivity. this, "http://api.com/login", stringEntity, "application/json", new JsonHttpResponseHandler () {@ Override Public void onSuccess (JSONObject jsonObject) {super. onSuccess (jsonObject) ;}}) ;}catch (JSONException e) {e. printStackTrace ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} officially recommended method of use, using a static request object, let's take a look at the official example of an API to access Twitter: Usage: Due to network requests, do not forget to add permissions: [html] <uses-permission android: name = "android. permission. INTERNET "/> other functions, such as uploading and downloading files, can be viewed on the official website. I will not repeat them here!

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.