One of the ways that Android communicates with the server (JSON) is inefficient and not safe.

Source: Internet
Author: User

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 (); }     }


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.