当前位置: 首页 > 编程笔记 >

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

师冥夜
2023-03-14
本文向大家介绍Android开发之登录验证实例教程,包括了Android开发之登录验证实例教程的使用技巧和注意事项,需要的朋友参考一下

本文所述实例源自一个项目开发中的登录验证功能,具体的要求就是,在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程序设计有所帮助。

 类似资料:
  • 本文向大家介绍Android开发实例之登录界面的实现,包括了Android开发实例之登录界面的实现的使用技巧和注意事项,需要的朋友参考一下 本文要演示的Android开发实例是如何完成一个Android中的miniTwitter登录界面,下面将分步骤讲解怎样实现图中的界面效果,让大家都能轻松的做出美观的登录界面。        miniTwitter登录界面效果图        先贴上最终要完成的

  • 本文向大家介绍Android miniTwitter登录界面开发实例,包括了Android miniTwitter登录界面开发实例的使用技巧和注意事项,需要的朋友参考一下 本文要演示的Android开发实例是如何完成一个Android中的miniTwitter登录界面,下面将分步骤讲解怎样实现图中的界面效果,让大家都能轻松的做出美观的登录界面。 先贴上最终要完成的效果图: miniTwitter登

  • 本文向大家介绍Android开发之注册登录方法示例,包括了Android开发之注册登录方法示例的使用技巧和注意事项,需要的朋友参考一下 本文所述,继续上一篇关于Android端向服务器端发送数据的方法进一步完善注册登录的方法,由于版本问题出现一点瑕疵,今天经过调试已经解决,在这里给大家介绍一下。 在Android4.0以后版本的对于网络权限要求变得严格,致使上一篇所述的案例无法将数据发送到服务器端

  • 本文向大家介绍JavaScript登录验证基础教程,包括了JavaScript登录验证基础教程的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了js登录验证的具体代码,供大家参考,具体内容如下 1.<script></script>的三种用法:    1.放在<body>中    2.放在<head>中    3.放在外部JS文件中 document.getElementById('d

  • 本文向大家介绍Android开发中通过手机号+短信验证码登录的实例代码,包括了Android开发中通过手机号+短信验证码登录的实例代码的使用技巧和注意事项,需要的朋友参考一下 首先,需要一个电话号码,目前很多账户都是将账户名设置成手机号,然后点击按钮获取手机验证码。 其次,你需要后台给你手机短信的验证接口,各个公司用的不一样,这个身为前端,不需要你来考虑,你只要让你后台给你写好接口,你直接调用就好

  • 本文向大家介绍Qt for Android开发实例教程,包括了Qt for Android开发实例教程的使用技巧和注意事项,需要的朋友参考一下 本文讲述了使用Qt5.3.0开发Android应用的方法,由于官方资料较少,此处记录开发过程遇到的问题及解决方法。具体步骤如下: 1.Android平台的视频播放,只能使用qml的MediaPlayer 2.qml中控件的路径必须加file://  例如: