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