One of the methods of communication between Android and Server (JSON) is not very efficient and security is not good

Source: Internet
Author: User

HTTP communication. Take XML or JSON as the carrier. Communicate data to each other.

Android provides a standard Java interface--httpurlconnection interface for HTTP network communication. and the 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, two compared to the other words. Post transmits a larger amount of data and is more secure. So when you do a data query. I'll use get way. And when it comes to adding, changing, or deleting data, I use the Post method
Here is the code based on the HttpClient interface, which communicates in the way 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); ④ inferred 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 number to be passed list<namevaluepair> params = new arraylist<namevaluepair> ();        ④ joins the Params.add (new Basicnamevaluepair ("ParentID", "0″") to be passed;       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);//⑨ Infer if the request is 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 (); }     }


One of the methods of communication between Android and Server (JSON) is not very efficient and security is not good

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.