The use of Android-async-http

Source: Internet
Author: User

1.android-async-http use of Get and post requests

1 /*2 * Use of Get and post requests for 1.android-async-http3  */4  Public classMainactivityextendsactionbaractivity {5 6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Ten         //asynchttpget (); One asynchttppost (); A     } -  -     //--------------------Post () mode to request a network--------------------------------------------- the     Private voidAsynchttppost () { -  -String url = "Http://apis.juhe.cn/mobile/get?"; -Asynchttpclient client =Newasynchttpclient (); +Requestparams params =Newrequestparams (); -Params.put ("Phone", "13666666666"); +Params.put ("Key", "335adcc4e891ba4e4be6d7534fd54c5d"); AClient.post (URL, params,NewAsynchttpresponsehandler () { at @Override -              Public voidonsuccess (String content) { -                 //TODO auto-generated Method Stub -                 Super. onsuccess (content); -Toast.maketext (mainactivity. This, content, 1). Show (); -             } in  - @Override to              Public voidonfailure (throwable error) { +                 //TODO auto-generated Method Stub -                 Super. OnFailure (error); theToast.maketext (mainactivity. This, "Request Failed", 1). Show (); *             } $ Panax Notoginseng         }); -  the     } +  A     //--------------------Get () mode to request a network--------------------------------------------- the     Private voidAsynchttpget () { +Asynchttpclient client =Newasynchttpclient (); -String url = "HTTP://APIS.JUHE.CN/MOBILE/GET?PHONE=13666666666&KEY=335ADCC4E891BA4E4BE6D7534FD54C5D"; $Client.get (URL,NewAsynchttpresponsehandler () { $  - @Override -              Public voidonsuccess (String content) { the                 //TODO auto-generated Method Stub -                 Super. onsuccess (content);WuyiToast.maketext (mainactivity. This, content, 1). Show (); the             } -  Wu @Override -              Public voidonfailure (throwable error) { About                 //TODO auto-generated Method Stub $                 Super. OnFailure (error); -Toast.maketext (mainactivity. This, "Request Failed", 1). Show (); -             } -         }); A  +     } the  -}

2.android-async-http two-time package for callback logic

1  Public classRequestutils {2 3      Public StaticAsynchttpclient client=Newasynchttpclient ();4      Public Static voidclientget (String URL, netcallback cb) {5 client.get (URL, cb);6     }7     8 9      Public Static voidclientpost (String URL, requestparams params,netcallback cb) {Ten client.post (Url,params, CB); One     } A}

1  Public Abstract classNetcallbackextendsAsynchttpresponsehandler {2 @Override3      Public voidOnStart () {4         //TODO auto-generated Method Stub5         Super. OnStart ();6 7SYSTEM.OUT.PRINTLN ("--->> request start, Popup progress bar");8     }9 Ten @Override One      Public voidonsuccess (String content) { A         //TODO auto-generated Method Stub -         Super. onsuccess (content); - onmysuccess (content); theSYSTEM.OUT.PRINTLN ("--->> request success, hide progress bar" +content); -     } -  - @Override +      Public voidonfailure (throwable error) { -         //TODO auto-generated Method Stub +         Super. OnFailure (error); A onmyfailure (error); atSYSTEM.OUT.PRINTLN ("--->>failure"); -     } -  -      Public Abstract voidonmysuccess (String result); -  -      Public Abstract voidonmyfailure (throwable error); in}

1 /*2  * 3 * 2.android-async-http callback Logic two-time package4  * 5  * 6  * */7  Public classMainactivityextendsactionbaractivity {8  9 @OverrideTen     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); -         //asynchttpget (); - asynchttppost (); the     }  -     //--------------------Post () mode to request a network--------------------------------------------- -     Private voidAsynchttppost () { -      +String url = "Http://apis.juhe.cn/mobile/get?"; -      +Requestparams params=Newrequestparams (); AParams.put ("Phone", "13666666666"); atParams.put ("Key", "335adcc4e891ba4e4be6d7534fd54c5d"); -Requestutils.clientpost (URL, params,NewNetcallback () { -              - @Override -              Public voidonmysuccess (String result) { -                 //TODO auto-generated Method Stub inToast.maketext (mainactivity. This, result, 1). Show (); -             } to              + @Override -              Public voidonmyfailure (throwable error) { the                 //TODO auto-generated Method Stub *Toast.maketext (mainactivity. This, "Request Failed", 1). Show (); $             }Panax Notoginseng         }); -          the          +          A}

The use of Android-async-http

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.