This is a great third-party open source class library that sends HTTP requests for Android development.
Its network requests are automatically executed in a non-UI thread, and you don't need any extra action (such as manually new thread, etc.). Official website of the project: http://loopj.com/android-async-http/, corresponding GitHub address:https://github.com/loopj/android-async-http It is an asynchronous callback-based HTTP client built specifically for Android on the httpclient basis of Apache. All requests occurs outside the UI thread, and callback occurs in the thread that created it, and the handler sending message mechanism is applied to Android. You can also apply asynchttpclient to a service or background thread, and the library code will automatically recognize the context it is running on. The features include: 1. Sends an asynchronous HTTP request to process the response; 2 in an anonymous callback object. HTTP requests occur outside the UI thread; 3. The internal thread pool is used to handle concurrent requests; 4. The Get/post parameter is constructed through the Requestparams class. 5. Built-in multipart file upload, no third-party library support required; 6. Streaming JSON uploads, no additional libraries required; 7. able to handle circular and relative redirects; 8. Compared to the size of your app, the library has a small size and everything is only 90kb; 9. Automatic Intelligent request retry mechanism in a variety of mobile connected environments; 10. Automatic gzip response decoding; 11. Built-in various forms of response parsing, there are native byte stream, String,json object, and even can write response to the file; 12. Persistent cookie preservation, internal implementation using Android SHAREDPREFERENCES; 13. Integrated through Basejsonhttpresponsehandler and various JSON libraries; 14. Support sax parser; 15. supports a variety of languages and content encodings, not just UTF-8. 1. Create a asynchttpclient; 2. (optional) Set the request parameters through the Requestparams object; 3. Call a Get method of asynchttpclient to pass yourRequired (Success and failure) callback interface implementation, is generally anonymous internal class, the implementation of Asynchttpresponsehandler, the class library itself also provides a lot of response handler, you generally do not need to create one yourself. Code Example:
1 Importcom.loopj.android.http.*;2 3 Public classtwitterrestclient {4 Private Static FinalString base_url = "http://api.twitter.com/1/";5 6 Private StaticAsynchttpclient client =Newasynchttpclient ();7 8 Public Static voidget (String URL, requestparams params, Asynchttpresponsehandler responsehandler) {9 client.get (Getabsoluteurl (URL), params, responsehandler);Ten } One A Public Static voidpost (String URL, requestparams params, Asynchttpresponsehandler responsehandler) { - client.post (Getabsoluteurl (URL), params, responsehandler); - } the - Private Staticstring Getabsoluteurl (String relativeurl) { - returnBase_url +Relativeurl; - } +}
1 Importorg.json.*;2 Importcom.loopj.android.http.*;3 4 classTwitterrestclientusage {5 Public voidGetpublictimeline ()throwsjsonexception {6Twitterrestclient.get ("Statuses/public_timeline.json",NULL,NewJsonhttpresponsehandler () {7 @Override8 Public voidOnsuccess (intStatusCode, header[] headers, jsonobject response) {9 //If The response is jsonobject instead of expected JsonarrayTen } One A @Override - Public voidOnsuccess (intStatusCode, header[] headers, Jsonarray timeline) { - //Pull out the first event on the public timeline theJsonobject firstevent = timeline.get (0); -String Tweettext = firstevent.getstring ("text"); - - //Do something with the response + System.out.println (tweettext); - } + }); A } at}
Android Open source Framework Android-async-http