About the use of android-async-http, encapsulating network requests

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px">first, Async was like Wirelessly's own asynctask, but it was simpler.<span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="color:#ff6666"><br></span></span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px">Asynchronous callback-based HTTP clients are built for Android and are based on the Apache HttpClient library. All requests are located outside the Application's main thread UI, But any callback logic will execute callbacks on the same threads, using the Android handler to create the message delivery.</span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong>First encapsulate the asynchttpclient, the core class in this JAR package</strong></span></span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"></span></span></span></span></p></p><pre name="code" class="java"><pre name="code" class="java">public class Asynchttprequestclient { private static Asynchttpclient client = new Asynchttpclient (); public static void Get (String url, requestparams params, asynchttpresponsehandler responsehandler) { Client.get (url, params, responsehandler); } public static void Post (String url, requestparams params, asynchttpresponsehandler responsehandler) { Client.post (url, params, responsehandler); } }</pre></pre><strong><strong><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong>asynchttpresponsehandler, responsible for handling callbacks in network requests</strong></span></span></span></span></strong></strong><p><p></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong>requestparams, responsible for the transfer of parameters</strong></span></span></span></span></p></p><p><p><br><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"></span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong>Then encapsulate the class of your own <span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong>Asynchttpresponsehandler</strong></span></span></span></span></strong></span></span></span></span></strong></span></span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong></strong></span></span></span></span></p></p><pre name="code" class="java">public class Jsonobjectresponsehandler extends jsonhttpresponsehandler{private Activity parent = Null;public Jsonobjectresponsehandler (Activity Act) {super ("utf-8"); this.parent = act;} Override this method at the call to do what you want to do public void Onjsonok (jsonobject Response) {}//when Parsing json The problem is encountered public void Onjsonfail (throwable throw Able) {toast.maketext (this.parent, "json parsing exception", toast.length_short). show (); LOG.E ("net error", "error", throwable);} 200 successful and return value can be converted to jsonpublic void onsuccess (int statusCode, header[] headers, jsonobject response) {try {//toast.maketex T (this.parent, response.tostring (), toast.length_short). show (); System.out.println (response.tostring ()); Do a write yourself jsonobject headerjsonobject = response.getjsonobject ("header"); int err = Headerjsonobj Ect.getint ("err"); If (err = = 0) { this.onjsonok (response.getjsonobject ("body"));} Else{toast.maketext (this.parent, headerjsonobject.getstring ("errmsg"), Toast. Length_short). Show ();}} Catch (jsonexception e) {e.printstacktrace (); Onjsonfail (throwable throwable)  }};<span style= "font-family:palatino linotype,palatino,book Antiqua,s erif; " >//request failed or the return value cannot be converted to json</span>public void onfailure (int statusCode, header[] headers, String responsestring, Thr Owable throwable) {System.out.println (responsestring);//log.v ("error", responsestring); }}</pre><br><br><pre><pre></pre></pre><span style="font-size:14px"><span style="font-size:14px"></span></span><p><p></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"></span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"><strong>finally, use it.</strong></span></span></span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Palatino Linotype,Palatino,Book Antiqua,serif"><span style="font-size:15px; line-height:22px"><span style="font-size:24px"></span></span></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"> Asynchttprequestclient.post (url, params,new jsonobjectresponsehandler (this) {public void Onjsonok (jsonobject Response) { //network request succeeded and got the correct result you can now go to parse response } });</pre></pre><strong><strong><br><br></strong></strong><p><p></p></p><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>About the use of android-async-http, encapsulating network requests</p></p></span>

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.