栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

布局文件如下:


  
    
    
    
    
    

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

public class NewsService {
  
  public static boolean save(String name, String password){
    String path = "http://192.168.1.104:8080/Register/ManageServlet"; 
    Map student = new HashMap();
    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;
  }
  
  private static boolean SendGETRequest(String path, Map 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 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进行网络权限配置:



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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164440.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号