欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之登录验证实例教程

程序员文章站 2022-04-30 23:03:40
本文所述实例源自一个项目开发中的登录验证功能,具体的要求就是,在android端输入用户名和密码,在服务器端验证mysql数据库中是否有此用户,实现之前当然首要的是,如何使...

本文所述实例源自一个项目开发中的登录验证功能,具体的要求就是,在android端输入用户名和密码,在服务器端验证mysql数据库中是否有此用户,实现之前当然首要的是,如何使android端的数据发送到服务器端,具体的实现方法如下:

服务器端: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("用户名:"+name+" 密码:"+password);
  }
  public void dopost(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
  }
}

在这里实现的仅仅是把用户端的数据在控制台打印出来,相信学过jsp开发的大神,剩下的数据验证应该不在话下,在此不再赘述。

接下来就是android端了:

主activity:mainactivity.java页面代码如下:

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();
      }
    }
  }
}

布局文件如下:

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

用于向服务器端发送数据的service(newsservice):

public class newsservice {
  /**
   * 登录验证
   * @param name 姓名
   * @param 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;
  }
  /**
   * 发送get请求
   * @param path  请求路径
   * @param student  请求参数
   * @return 请求是否成功
   * @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=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() == 200){
      return true;
    }
    return false;
  }
}

因为需要连接网络,一定要在androidmanifest.xml进行网络权限配置:

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

至此基本已经完成android向服务器端发送数据,希望本文实例对大家的android程序设计有所帮助。