Data interaction between Android client and server (one, login server side)

Source: Internet
Author: User

Profile

The Android app must use network data interactions to implement many features, such as sign-in registration, commenting, and so on. So after learning this part of the content, it is the most common sign-up process as an example of sorting out, but also convenient for me as the novice can quickly learn to get started.

As expected, enter your phone number and password, click the login button, upload the data to the server side for verification, and then feedback the results.

Blog 122 articles, the first is the server side, the second is the Android client
And the server side is divided into user model, Userdao, loginservlet three parts, only write the users login verification process The simplest implementation logic, for reference only.

User model

The most important attribute of user model is the account (which is now the mobile number) and the account password, and also defines the key, which is used to send and take parameters in the URL.

 Public  class User implements Serializable{    Private intIdPrivateString PhoneNumber;PrivateString password;PrivateString UserName;PrivateString EmailAddress;PrivateDate Registertime;//Key when used for URL-pass and parameter-taking     Public StaticString PhoneNumber ="PhoneNumber"; Public StaticString PASSWORD ="PassWord"; Public StaticString USERNAME ="UserName";//Constructors     Public User(String phonenumber,string password,string userName) { This. PhoneNumber = PhoneNumber; This. Password = password; This. userName = UserName; Registertime =NewDate (); }//Property Get and set methods are no longer posted}
Userdao

There are no complicated operations and query pairs involved in the database, a simple ArrayList array is used as the data source, and the Islogincheck method iterates through the values to determine the success of the login verification.

 Public classUserdao { Public Static void Main(string[] args) {//local validation main functionUserdao Userdao =NewUserdao (); System. out. println (Userdao.isloginok ("123456","abc123456")); }//assumptions of the data source     PublicArraylist<user> users; Public Userdao() {//TODO auto-generated constructor stubUsers =NewArraylist<user> (); Users.add (NewUser ("123456","abc123456","Codingma")); Users.add (NewUser ("654321","Abcdf123","Codemayq")); }//Validation function     PublicBooleanIsloginok(String phonenumber,string password) { for(User user:users) {if(User.getphonenumber (). Equals (PhoneNumber)) {if(User.getpassword (). Equals (password)) {return true; }            }        }return false; }}
Loginservlet

Loginservlet is just a normal servlet, in the Dopost method, the parameters in the URL are extracted, and then called Userdao in the Isloginok method to verify, return OK or wrong the result value

 Public  class loginservlet extends httpservlet {     Public Loginservlet() {Super(); } Public void Doget(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {response.setcontenttype ("Text/html");        PrintWriter out = Response.getwriter (); Out.println ("GET");        Out.flush ();    Out.close (); } Public void DoPost(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {response.setcontenttype ("Text/html"); PrintWriter out = Response.getwriter ();//Remove the account password parameter from the URLString PhoneNumber = Request.getparameter (User.phonenumber);        String PassWord = Request.getparameter (User.password); Userdao Userdao =NewUserdao ();//Verification process        if(Userdao.isloginok (PhoneNumber, PassWord)) {Out.println ("OK"); }Else{Out.println ("Wrong");        } out.flush ();    Out.close (); }}
Summarize

The server-side section is divided into three parts, the user class, for the data validation of the Userdao class, to receive processing requests and return the results of the Loginservlet, the call relationship is clear, not particularly complicated.

Android client code see the next chapter
Reprint please specify the source http://blog.csdn.net/u012145166/article/details/51335187

Data interaction between Android client and server (one, login server side)

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.