Android Development Login Verification Example Tutorial _android

Source: Internet
Author: User
Tags gettext

The example in this article originates from the login verification function in the development of a project, the specific requirement is to enter the username and password on the Android side, and verify that the MySQL database has this user in the server side, before the realization of course, how to make the data on the Android side to the server side, The specific implementation methods are as follows:

Server-side: Manageservlet.java code is as follows:

public class Manageservlet extends HttpServlet {public
  void doget (HttpServletRequest request, HttpServletResponse Response)
      throws Servletexception, IOException {
    request.setcharacterencoding ("Utf-8");
    Response.setcharacterencoding ("Utf-8");
    String name = Request.getparameter ("name");
    String Password = request.getparameter ("password");
    System.out.println ("User name:" +name+ "Password:" +password);
  }
  public void DoPost (HttpServletRequest request, httpservletresponse response)
      throws Servletexception, IOException {
  }
}

Here is to achieve only the user side of the data in the console print out, I believe that learned the JSP development of the Great God, the rest of the data validation should be a cinch, here no longer repeat.

And then there's the Android side:

The main Activity:MainActivity.java page code is as follows:

public class Mainactivity extends activity {private EditText textname = null;
  Private EditText Textpassword = null;
  Private button button = null;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
     
    Setcontentview (R.layout.activity_main);
    Textname = (edittext) Findviewbyid (r.id.name);
    Textpassword = (edittext) Findviewbyid (R.id.password);
     
    Button = (button) Findviewbyid (R.id.button);
     
  Button.setonclicklistener (New Mybuttonlistener ());
    Class Mybuttonlistener implements onclicklistener{Boolean result=false;
    String name;
    String password;
        public void OnClick (View v) {try {name = Textname.gettext (). toString ();
        name = new String (name.getbytes ("iso8859-1"), "UTF-8");
        Password = Textpassword.gettext (). toString ();
      Password = new String (password.getbytes ("iso8859-1"), "UTF-8");
     catch (Unsupportedencodingexception E1) {   TODO auto-generated Catch block E1.printstacktrace ();
      try {result = Newsservice.save (Name,password);
      catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
      } if (Result) {Toast.maketext (Mainactivity.this, R.string.ok, Toast.length_short). Show ();
      }else{Toast.maketext (Mainactivity.this, R.string.error, Toast.length_short). Show ();

 }
    }
  }
}

The

Layout file is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context" = "${relativepackage}.${activityclass}" > <linearlayout android:layout_width= "fill_parent" android:layou t_height= "fill_parent" android:orientation= "vertical" > <textview android:layout_width= "Wrap_conte" NT "android:layout_height=" Wrap_content "android:text=" @string/name "/> <edittext android:id= "@+id/name" android:layout_width= "fill_parent" android:layout_height= "wrap_content" "android:hint=" @strin G/playname "android:singleline=" true "/> <textview android:layout_width=" Wrap_content "a ndroid:layout_height= "Wrap_content" android:text= "@string/password"/> <edittext android:id= "@+id/p Assword "Android:layout_width=" fill_parent"Android:layout_height=" wrap_content "android:password= true" android:hint= "@string/playpass" and 
      Roid:singleline= "true"/> <button android:id= "@+id/button" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:onclick= "android:text=" @string/submit "/>"

 ;/linearlayout> </RelativeLayout>

Service (Newsservice) for sending data to the server side:

public class Newsservice {/** * login authentication * @param name * @param password Password * @return/public static Boolean Save (string name, string password) {string path = "Http://<span style=" color: #ff0000; " 
    ><strong>192.168.1.104</strong></span>:8080/register/manageservlet ";
    map<string, string> student = new hashmap<string, string> ();
    Student.put ("name", name);
    Student.put ("password", password);
    try {return Sendgetrequest (path, student, "UTF-8");
    catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
  return false; /** * Send GET Request * @param path Request route * @param student Request parameter * @return Request success * @throws Exception * * p
    Rivate Static Boolean Sendgetrequest (string path, map<string, string> student, String ecoding) throws exception{ HTTP://127.0.0.1:8080/REGISTER/MANAGESERVLET?NAME=1233&AMP;PASSWORD=ABC StringBuilder url = new StriNgbuilder (path);
    Url.append ("?");
      For (map.entry<string, string> map:student.entrySet ()) {Url.append (Map.getkey ()). Append ("=");
      Url.append (Urlencoder.encode (Map.getvalue (), ecoding));
    Url.append ("&");
    } Url.deletecharat (Url.length ()-1);
    System.out.println (URL);
    Httpsurlconnection conn = (httpsurlconnection) New URL (Url.tostring ()). OpenConnection ();
    Conn.setconnecttimeout (100000);
    Conn.setrequestmethod ("get");
    if (conn.getresponsecode () = =) {return true;
  return false;

 }
}

Because you need to connect to the network, be sure to configure the network permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.INTERNET"/>

So far the basic has completed Android to the server to send data, I hope this example of this article on the Android design help.

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.