Data Interaction Process between Android client and server

Source: Internet
Author: User
The process of making a portal website: first, the webpage designer only designs the website, that is, the website display in the browser, then, the front-end staff compile HTML + CSS + js to achieve the dynamic effect of the website, such as the drop-down display of the navigation bar, and then update some Ajax information, and then use the backgroundProgramFor example, JSP, PHP,. net, and other languages combine the information in the database with the front-end page. In this way, a station is created. The android client is also composed of UI designers, UI front-ends, and programs. In the same way as the program side, HTML5 + css3 has recently become popular, many companies also need HTML5 + css3 for Android development. Hey, grab my job. Although he understands HTML + CSS. Hey hey, you may not be clear about this explanation. It is necessary for solid wood to figure out the website construction process. Here we just give you some knowledge and do not need to figure it out.
Basic knowledge : We need to know, HTTP The protocol is based on TCP Protocol, and TCP The Protocol is a connection and reliable transmission protocol. If it is lost, it will be re-transmitted. So there will be no data loss. While HTTP There are three methods for the Protocol, Get, post, head Method, but onlyGet And Post Method, Get The method is to put the request parameters in the Request Header, so the request parameters are URL And Post The method is to put the request parameters in the data section, so in URL Invisible, Post It is relatively confidential, so when submitting important information Httppost For example, the account and password are registered. Httppost Because the account and password need to be kept confidential, and the android bus is searched for in hundreds of times, this type is displayed in the address bar of the browser. URL , Where WD = xxx It is an encrypted Android bus. Other parameters are required. Http://www.baidu.com? WD = % E5 % AE % 89% E5 % 8d % 93% E5 % B7 % B4 % E5 % A3 % AB & rsv_spt = 1 & ISSP = 1 & rsv_bp = 0 & Ie = UTF-8 & Tn = baiduhome_pg & inputt = 11996 . in B/S mode, B indicates the browser, and S indicates the server. In the communication between the browser and the server, because the B/S is not always connected, the cookie mechanism to identify the operated object. When a user clicks a connection or button, the browser sends a request to the server and the server receives the request, it then parses the content requested by the user, finds the relevant resources, returns the data to the browser, parses the data by the browser, and then displays the page that the user needs to see. Therefore, Android and the B/S mode is similar. It is actually the process of sending a request, receiving data, parsing the data, and displaying it on the mobile phone. There is no big difference, who makes us use the Internet and the TCP/IP protocol. It should be understood that all operations are the process of requesting data from the server.
 first, we need to be familiar with Android httpget and httppost requests. Apache already provides two classes: httpget and httppost. The usage of the two classes is different. do not describe the  Code : 1. use httpget to request the Baidu home page: // use the httpget method to pass the Baidu home page into httpget hettpget = new httpget ("http://www.baidu.com /"); // use the default httpclienthttpclient Hc = new defaulthttpclient (); try {// execute the httpget method and obtain the returned response httpresponse response = hc.exe cute (hettpget ); // If the response code is 200, the request is obtained successfully. Otherwise, if (response. getstatusline (). getstatuscode () = 200) {// s is the obtained HTML code string S = entityutils. tostring (response. getentity (); system. out. println (s) ;}} catch (clientprotocolexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}

then, a httpget request: the Baidu homepage is requested, and the server returns all HTML code, only parsed by the browser HTML function, set HTML code is converted to the displayed page. HTML code.

// Use httppost to send the request httppost = new httppost (URL); // use namevaluepaira to save the list of parameters to be passed in the request <namevaluepair> paramas = new arraylist <namevaluepair> (); paramas. add (New basicnamevaluepair ("A", "A"); try {httpresponse; // put namevaluepair into httppost Request body httppost. setentity (New urlencodedformentity (paramas, HTTP. utf_8); // execute the http post request httpresponse = new defaulthttpclient(cmd.exe cute (httppost); // If the response code is 200, the request is obtained successfully; otherwise, the request is returned if (httpresponse. getstatusline (). getstatuscode () == 200) {string S = entityutils. tostring (httpresponse. getentity () ;}} catch (unsupportedencodingexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (clientprotocolexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}

In this way, Httppost Request. Simple. All data retrieval operations are basically performed in Httpget And Httppost After a request is sent, the server will receive a request, and the server will receive the request through a method.   In Android The parameters sent by the client are parsed by the background program, and the required information is found through the database. PHP The background processes data JSON Data format returned Httpresponse Of Entity And then Entitiyutils. tostring MethodEntity Convert String Object, and then the obtained String The object is JSON Data. The next thing is JSON Data parsing. Finally, let's briefly talk about the process of dynamic website construction: There are two types of pages: Static and Dynamic. Currently, static websites are basically gone, and they are all dynamic, static pages are all usedHTML + CSSWrite, you can only open another page from one page, without the data interaction process with the server, just simple loading static pages, there will be no operations such as data deletion and modification from the database, and no need to use the template generator to generate a file, and then return the file content to the browser, the dynamic website creation process is like this: first, the server sends a request to the server, then the server queries data through the database, and then combines the queried data with the template builder to generate. PhpFile, then the browser willPHPFile resolvedHtmlThen it is displayed in the browser.   Welcome Android Developers to join the group for common progress. Nanjing group 220818530, Wuhan group 121592153, Hangzhou State group 253603803, Xiamen group 253604146, Hunan group 217494504, Dalian group 253672904
Qingdao group 257925319
Related Article

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.