最近在做一个小项目,项目开发中需要实现一个登录验证功能,具体的要求就是,在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://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 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;
} }

  红色的为自己电脑的IP地址。 

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

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

  写到这里基本已经把Android向服务器端发送数据,给大家分享完毕,如有不对的地方还望指正。

Android开发之登录验证的更多相关文章

  1. android loginDemo +WebService用户登录验证

        android loginDemo +WebService用户登录验证 本文是基于android4.0下的loginActivity Demo和android下的Webservice实现的.l ...

  2. [Android开发]- MVC的架构实现登录模块-1

    本系列博客主要展示一下,在C-S(Client - Server)系统开发当中,如何使用MVC的架构来实现安卓端的一个登录验证的模块.如果你能有基本的数据库开发,WEB开发,和安卓开发的知识,那么理解 ...

  3. Android 实现http通信(servlet做服务器端) HttpClient、HttpURLConnection实现登录验证

    一,写好服务器端 在eclipse或其它javaee开发工具中新建一个web项目(我这里的项目名是:Android),建一个servlet(我这里的servlet名是:LoginServlet),模拟 ...

  4. Android开发——程序锁的实现(可用于开发钓鱼登录界面)

    1. 程序锁原理 1.1 实现效果: 在用户打开一个应用时,若此应用是我们业务内的逻辑拦截目标,那就在开启应用之后,弹出一个输入密码的界面,输入密码正确则进入目标应用.若不输入直接按返回键,则直接返回 ...

  5. android开发学习——facebook第三方登录,看了你不会后悔

    给APP用原生android进行facebook第三方登录. 我们做一件事情,首先得了解其原理,这样才不会迷茫,才知道自己做到什么程度了,心里才会有底. 所以,第一步,了解第三方登录的原理:下面贴一些 ...

  6. Android开发实例之miniTwitter登录界面的实现

    原文: http://www.jizhuomi.com/android/example/134.html 本文要演示的Android开发实例是如何完成一个Android中的miniTwitter登录界 ...

  7. 单机搭建Android开发环境(二)

    前文介绍了如何优化SSD和内存,以发挥开发主机的最佳性能,同时提到在SSD上创建虚拟机.为什么不装双系统呢?双系统性能应该会更好!采用Windows+虚拟机的方式,主要是考虑到安卓开发和日常办公两方面 ...

  8. Xamarin Anroid开发教程之验证环境配置是否正确

    Xamarin Anroid开发教程之验证环境配置是否正确 经过前面几节的内容已经把所有的编程环境设置完成了,但是如何才能确定所有的一切都处理争取并且没有任何错误呢?这就需要使用相应的实例来验证,本节 ...

  9. Android开发中,那些让您觉得相见恨晚的方法、类或接口

    Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...

随机推荐

  1. swift的运算符

    1.什么是运算符?它有什么作用? 运算符是一种特定的符号或者表达式.它用来验证.修改.合并变量. 2.运算符有哪些? 运算符有很多,很多朋友学的很烦.这里我依据它的作用把它分为几块来介绍: a:赋值运 ...

  2. jdk1.6 webService 客户端代码生成和测试

    参数:测试webService的地址:http://10.113.11.1:9090/enocpService/buildingEngService?wsdl 1,eclipse中新建一个项目, 2, ...

  3. 界面显示两个ListView

    1.List界面布局 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:an ...

  4. hdu 5661 Claris and XOR

    Claris and XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. Logger的等级输出

    转自:http://zhidao.baidu.com/link?url=7HnNxxUei6m3X3JOLfK4yShElbu5xwvU9Z7ipBAaIvQOi3Bc30N-7o7JgfnZDEg3 ...

  6. ABP框架详解(五)Navigation

    ABP框架中的Navigation功能用于管理业务系统中所有可用的菜单导航控件,通常在业务系统的首页会有一个全局性的导航菜单,JD商城,天猫,猪八戒网莫不如是.所以为方便起见,Navigation功能 ...

  7. 如何安装Docker UCP

    parallels@ubuntu:~/Downloads/Docker$ ls DTR UCP parallels@ubuntu:~/Downloads/Docker$ sudo openssl re ...

  8. svn忽略eclipse自动生成的文件

    工程目录下右键,选择“TortoiseSVN”——“Settings”菜单 选择“General”,在“Global ignore pattern”输入框的最前面添加 .settings .class ...

  9. 可在广域网部署运行的QQ高仿版 -- GG叽叽V1.8(源码)

    距离的GG 1.0发布已经三周了,这三周内,我利用业余时间为GG增加了视频聊天的功能.个人觉得进展有些缓慢,主要是因为大多数时间都花在了UI上.由于本人不会PS,所以图片素材都是从网上一个一个搜下来的 ...

  10. 关于CAP定理的个人理解

    CAP定理简介 在理论计算机科学中,CAP定理(CAP theorem),又被称作布鲁尔定理(Brewer's theorem),它指出对于一个分布式计算系统来说,不可能同时满足以下三点: 一致性(C ...