Login verification for Android development and android development verification

Source: Internet
Author: User

Login verification for Android development and android development verification

Recently, I am working on a small project. in project development, I need to implement a login verification function. The specific requirement is to enter the user name and password on the Android terminal, verify whether the MySQL database has this user on the server. before implementation, the first thing is how to send Android data to the server:

Server: 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, we only print the user-side data on the console. I believe that I have learned about jsp development, and I will not go into details about the remaining data verification.

Next is the Android terminal:

Main activity: MainActivity. java

public class MainActivity extends Activity {private  EditText textname = null;private  EditText textpassword = null;private  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("ISO8859-1"), "UTF-8");password = textpassword.getText().toString();password = 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:layout_height="fill_parent"        android:orientation="vertical"    >    <TextView        android:layout_width="wrap_content"        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="@string/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_parent"        android:layout_height="wrap_content"        android:onClick=""        android:text="@string/submit"       />   </LinearLayout></RelativeLayout>

NewsService ):

Public class NewsService {/*** logon verification * @ param name * @ param 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 catch blocke. printStackTrace ();} return false ;} /*** send GET request * @ param path request path * @ param student request parameter * @ return request success * @ throws Exception */private stati C 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 () = 200) {return true;} return false ;}}

Red is the IP address of your computer.

To connect to the network, you must configure network permissions in AndroidManifest. xml:

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

Basically, Android has sent data to the server and shared it with you. If there is anything wrong, I hope to correct it.


Logon verification for android Development

Registration uses insert, and login queries are good.

Android program structure: login verification

In fact, pressing the return key is to close the current Activity, and pressing the home key is to enter the main interface of the mobile phone. The current application is not closed. When opening the application again, the interface that presses the home Key is displayed. According to your needs: The onBackPressed () method in the Activity can capture and process events that press the return key. Secondly, when a user logs on, the login information should be recorded locally, such: user name and password. When the user enters the application again, it is actually judged. If the login information is saved and the information is correct (this is an asynchronous request sent in the background ), of course, you can directly access the main interface. If no logon information is recorded or the password is incorrect, go to the logon page.

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.