Login Verification of Android development

Source: Internet
Author: User

Recently in a small project, project development needs to implement a login verification function, the specific requirement is to enter the user name and password on the Android side, on the server side to verify that the MySQL database has this user, before the implementation of course, first of all, how to make the Android side of the data sent to the server side, The specific implementation method:

Server side: Manageservlet.java

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 ("Username:" +name+ "  Password:" +password);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {}}

Here is the only implementation of the user-side data in the console print out, I believe that the development of JSP developed the great God, the rest of the data validation should be a little, here no longer repeat.

The next step is the Android side:

Main Activity:MainActivity.java

public class Mainactivity extends Activity {private EditText textname = null;private EditText Textpassword = Null;privat E button button = null, @Overrideprotected 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 ("I So8859-1 ")," UTF-8 ");p Assword = Textpassword.gettext (). toString ();p assword = new String (Password.getbytes (" iso8859-1 ")," UTF-8 ");} catch (Unsupportedencodingexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} try {result = Newsservice.save (Name,password);} catch (Exception e) {//TODO auto-generated CatCH Blocke.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 ();}}}

Layout file:

<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:layo ut_height= "fill_parent" android:orientation= "vertical" > <textview android:layout_width= "Wrap_co Ntent "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= "@st        Ring/playname "android:singleline=" true "/> <textview android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "@string/password"/> <edittext android:id= "@+id /password "Android:layout_width=" fill_pArent "android:layout_height=" Wrap_content "android:password=" true "android:hint=" @string/playpass " Android:singleline= "true"/> <button android:id= "@+id/button" android:layout_width= "Fill_pare    NT "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 verification * @param name name * @param password password * @return */public static Boolean save (String Name, string password) {string path = "http.//192.168.1.104: 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 CATC H Blocke.printstacktrace ();} return false;} /** * Send GET Request * @param path Request path * @param student request parameter * @return Request succeeded * @throws Exception */private Static Boolean Sendgetrequest (string path, map<string, string> Student, String ecoding) throws exception{///HTTP 127.0.0.1:8080/register/manageservlet?name=1233&password=abcstringbuilder 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;}}

Red for the IP address of your own computer.

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"/>

Write here basically has the Android to the server side to send data, to share the complete, if there is not the right place to see correct.

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.