<span id="Label3"></p><span style="color:#333333;font-family:宋体;font-size:14px;line-height:28px;background-color:#FFFFFF;">A while <span style="color:#333333;font-family:宋体;font-size:14px;line-height:28px;background-color:#FFFFFF;">ago just moved home, plus the company to send a new version, so has been relatively busy, the article is not updated for several weeks. I don't have time for this weekend, just hanging out in the Week.</span></span> found this great Third-party open source class library that sends HTTP requests for Android Development. in Android development, sending and processing HTTP requests is so common that our code is littered with all kinds of httpclient and related, smelly, long code, they exist in every corner of your code, and every time you see it is disgusting, And you just want the server to return a string or JSON to YOU. Every time I write this kind of code myself, I want to be able to simplify the process, perhaps 2 or 3 lines of code can be done. Because for the simplest case, I only need to provide the request url, success callback and/or failure callback, That's All. For this type of problem (demand), it can be said that Android-async-http provides a nearly perfect solution. by using it can greatly simplify your code, not only that, your code looks much more Elegant. I was attracted when I first saw it, especially the async keyword, as we know in this line, this is asynchronous execution, which means that its network request is 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 Here is a brief introduction: 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 a background thread, and the library code will automatically recognize the context it is running On. Its feature include: 1. Sends an asynchronous HTTP request to process response in an anonymous callback object; 2. HTTP requests occur outside the UI thread; 3. The internal thread pool is used to handle concurrent requests; 4. Get/post parameter construction, through RequestparaMs Class. 5. Built-in multipart file upload, no Third-party library support; 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's size is small, 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, even can write response to file; 12. Permanent cookie preservation, internal implementation using Android sharedpreferences; 13. Integrates with Basejsonhttpresponsehandler and various JSON libraries; 14. Support Sax parser; 15. supports a variety of languages and content encodings, not just UTF-8. Probably translated, these are just a general overview, specific details have to be used in the process of slowly feeling, learning. next, take a look at the application of Android-async-http to write code what it looks like. Simply put, you only need 3 steps, 1. Create a asynchttpclient; 2. (optional) set the request parameters through the Requestparams object; 3. Call Asynchttpclient a Get method, pass the callback interface implementation you need (success and failure), generally anonymous internal class , implement the asynchttpresponsehandler, The class library itself also offers a number of ready-made response handler, which you don't normally need to create yourself. to see how the code is written: Copy code 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, b yte[] errorresponse, throwable e) { //called When response HTTP status is "4XX" (eg. 401, 403 , 404) @Override public void Onretry (int retryno) { &NBSP ; //called when request is retried }}); Copy the code is not very concise, have not been shocked to? anyway, My first time I saw a kind of brief encounter feeling, This is the way I think of the night! You only need to implement Asynchttpresponsehandler by Anonymous internal classes, and what's even better is that you only need to override the methods of interest, such as onsuccess and OnFailure in General. This version of the Get method does not pass any parameters for the request, of course you can also pass various parameters through requestparams, as follows: Copy code asynchttpclient client = new Asynchttpclient (); Requestparams params = new Requestparams ();Params.put ("key", "value"); Params.put ("more", "data"); Client.get ("http://www.google.com", params, new Asynchttpresponsehandler () { @O Verride public void onsuccess (int statusCode, header[] headers, byte[] response) { &nbs P SYSTEM.OUT.PRINTLN (response); } @Override public void Onfailu Re (int statusCode, header[] headers, byte[] responsebody, throwable error) { LOG . D ("error", error); } }); Copy code above example is returned response directly is the original word throttling situation, if you need to return the results when a string treatment, then only need to implement an anonymous Texthttpresponsehandler on the line, It inherits from Asynchttpresponse and converts the native byte stream into a string object based on the specified encoding, code is as follows: Copy code asynchttpclient client = new Asynchttpclient (); Requestparams params = new Requestparams (); Params.put ("key", "value"); Params.puT ("more", "data"); Client.get ("http://www.google.com", params, new Texthttpresponsehandler () { @Ov Erride public void onsuccess (int statusCode, header[] headers, String response) {   ; SYSTEM.OUT.PRINTLN (response); } @Override public void Onfailu Re (int statusCode, header[] headers, String responsebody, throwable error) { LOG . D ("error", error); } }); Copy the code the same way that you can send a JSON request with the code as follows: copy code String URL = "https://ajax.googleapis.com/ajax/services/search/images"; Asynchttpclient client = new Asynchttpclient (); Requestparams params = new Requestparams (); Params.put ("q", "android"); Params.put ("rsz", "8"); Client.get (url, params, New Jsonhttpresponsehandler () { &NBSp @Override public void onsuccess (int statusCode, header[] headers, jsonobject response) {  ; //Handle resulting parsed JSON response here } @Override PU blic void onsuccess (int statusCode, header[] headers, jsonarray response) { //Handle resulting parse D JSON response here }}); Copy the code see no, the returned response has been automatically converted to jsonobject, of course, also support the Jsonarray type, override the version you Need.<p><p> Android-async-http asynchttpclient Introduction </p> </p></span>
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