HTTP communication, with XML or JSON as the carrier, communication data with each other.
Android for HTTP network communication, provides the standard Java interface--httpurlconnection interface, as well as Apache interface--httpclient interface. Among them I use more than httpclient this interface, because its function is more rich and effective.
At the same time, the HTTP communication is divided into post mode and get method, the two compared, post transmission of data is larger, security is also high. So when I do the data query, I use the Get method, and when I do the data add, modify or delete, I use the Post method
The following is code based on the HttpClient interface, which communicates in the form of Get and post.
(Get Mode)
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.login); TextView Text = (TextView) This.findviewbyid (r.id.text_http); String Httpurl = "Http://10.0.2.2:8080/SIM_SERVER/org/Org-list"; ①httpget Connection object HttpGet HttpRequest = new HttpGet (Httpurl); ② obtained HttpClient object HttpClient HttpClient = new Defaulthttpclient (); try {//③ request httpclient, get httpresponse httpresponse HttpResponse = Httpclient.execute (HttpRequest); ④ to determine if the request was successful if (Httpresponse.getstatusline (). Getstatuscode () ==httpstatus.sc_ok) {//⑤ Gets the returned string S Tring strresult = entityutils.tostring (Httpresponse.getentity ()); Text.settext (strresult); }else{Text.settext ("request Failed"); }} catch (Clientprotocolexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }} (post mode) protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); This.setcontentview (R.layout.login); TextView Text = (TextView) This.findviewbyid (r.id.text_http); ①http connection address String url = "Http://10.0.2.2:8080/SIM_SERVER/and/Android-test"; ② Get HttpPost Connection object HttpPost HttpPost = new HttpPost (URL); ③ uses Namevaluepair to save the post parameters to be passed list<namevaluepair> params = new arraylist<namevaluepair> (); ④ Add the parameter params.add to be passed (new Basicnamevaluepair ("ParentID", "0″"); Httpentity httpentity; try {//⑤ sets the character set httpentity = new Urlencodedformentity (params, "Utf-8″"); ⑥ sets the character set in Request Httppost.setentity (httpentity); ⑦ obtained HttpClient HttpClient HttpClient = new Defaulthttpclient (); ⑧ request sent and get response httpresponse HttpResponse = Httpclient.execute (httppost);//⑨ determine if the request was successful if (Httpresponse.get Statusline (). GetstaTuscode () ==httpstatus.sc_ok) {//⑩ Gets the return string strresult = Entityutils.tostring (httpresponse.ge TEntity ()); Text.settext (strresult); }else{Text.settext ("request Failed"); }} catch (Unsupportedencodingexception e) {e.printstacktrace (); } catch (Clientprotocolexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } }