Android Ajax Helper Class

Source: Internet
Author: User

Defined

1  PackageCMN;2 3 ImportAndroid.os.AsyncTask;4 ImportAndroid.widget.TextView;5 6 ImportCom.google.gson.Gson;7 8 ImportOrg.apache.http.HttpResponse;9 ImportOrg.apache.http.NameValuePair;Ten Importorg.apache.http.client.entity.UrlEncodedFormEntity; One ImportOrg.apache.http.client.methods.HttpPost; A Importorg.apache.http.impl.client.DefaultHttpClient; - ImportOrg.apache.http.protocol.HTTP; - Importorg.apache.http.util.EntityUtils; the  - Importjava.util.ArrayList; - Importjava.util.List; - ImportJava.util.Map; +  - ImportDZX.CN.DZX.R; +  A /** at * Created by Administrator on 2015/7/14. -  */ -  Public classNethelper { -      Public Static voidPost (String Url, Stringdict dict, Iajaxcallback callback) { -         NewNetexecuter (). Post (URL, Dict, callback); -     } in } -  to classNetexecuter { +     PrivateIajaxcallback Callback; -     Privatestringdict Dict; the  *      Public voidPost (String Url, Stringdict dict, Iajaxcallback callback) { $Callback =callback;Panax NotoginsengDict =dict; -  theAsynctask<string, stringdict, string> task =NewAsynctask<string, Stringdict, string>() { +  A @Override the             protected voidOnPreExecute () { +                 Super. OnPreExecute (); -             } $  $ @Override -             protectedstring Doinbackground (String ... params) { -  theString Myurl = params[0]; - WuyiHttpPost HttpRequest =NewHttpPost (myurl); theHttpResponse HttpResponse =NULL; -list<namevaluepair> param =NewArraylist<namevaluepair>(); Wu                 if(Dict! =NULL) { -                      for(FinalMap.entry<string, string>Entry:Dict.entrySet ()) { About  $Param.add (NewNamevaluepair () { - @Override -                              PublicString GetName () { -                                 returnEntry.getkey (); A                             } +  the @Override -                              PublicString GetValue () { $                                 returnEntry.getvalue (); the                             } the                         }); the                     } the                 } -  in                 Try { theHttprequest.setentity (Newurlencodedformentity (param, HTTP. Utf_8)); theHttpResponse =Newdefaulthttpclient (). Execute (httprequest); About  the                     intStatus =httpresponse.getstatusline (). Getstatuscode (); the  theString strresult =entityutils.tostring (Httpresponse.getentity ()); +  -                     returnstrresult; the}Catch(Exception e) {Bayi                     returne.getmessage (); the                 } the  -             } -  the @Override the             protected voidOnPostExecute (String result) { the                 Super. OnPostExecute (result); the  -Callback.exec (NewGson (). Fromjson (result, jsonmsg.class)); the             } the         }; the 94 Task.execute (URL); the     } the}
View Code

Call:

NULL New Iajaxcallback () {                    @Override                    publicvoid  exec (jsonmsg JM) {                        = ( TextView) Findviewbyid (R.id.textview);                        Txt.settext (JM. GetString ("Name"));                    } });

Android Ajax Helper Class

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.