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://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开发之登录验证的更多相关文章
- android loginDemo +WebService用户登录验证
android loginDemo +WebService用户登录验证 本文是基于android4.0下的loginActivity Demo和android下的Webservice实现的.l ...
- [Android开发]- MVC的架构实现登录模块-1
本系列博客主要展示一下,在C-S(Client - Server)系统开发当中,如何使用MVC的架构来实现安卓端的一个登录验证的模块.如果你能有基本的数据库开发,WEB开发,和安卓开发的知识,那么理解 ...
- Android 实现http通信(servlet做服务器端) HttpClient、HttpURLConnection实现登录验证
一,写好服务器端 在eclipse或其它javaee开发工具中新建一个web项目(我这里的项目名是:Android),建一个servlet(我这里的servlet名是:LoginServlet),模拟 ...
- Android开发——程序锁的实现(可用于开发钓鱼登录界面)
1. 程序锁原理 1.1 实现效果: 在用户打开一个应用时,若此应用是我们业务内的逻辑拦截目标,那就在开启应用之后,弹出一个输入密码的界面,输入密码正确则进入目标应用.若不输入直接按返回键,则直接返回 ...
- android开发学习——facebook第三方登录,看了你不会后悔
给APP用原生android进行facebook第三方登录. 我们做一件事情,首先得了解其原理,这样才不会迷茫,才知道自己做到什么程度了,心里才会有底. 所以,第一步,了解第三方登录的原理:下面贴一些 ...
- Android开发实例之miniTwitter登录界面的实现
原文: http://www.jizhuomi.com/android/example/134.html 本文要演示的Android开发实例是如何完成一个Android中的miniTwitter登录界 ...
- 单机搭建Android开发环境(二)
前文介绍了如何优化SSD和内存,以发挥开发主机的最佳性能,同时提到在SSD上创建虚拟机.为什么不装双系统呢?双系统性能应该会更好!采用Windows+虚拟机的方式,主要是考虑到安卓开发和日常办公两方面 ...
- Xamarin Anroid开发教程之验证环境配置是否正确
Xamarin Anroid开发教程之验证环境配置是否正确 经过前面几节的内容已经把所有的编程环境设置完成了,但是如何才能确定所有的一切都处理争取并且没有任何错误呢?这就需要使用相应的实例来验证,本节 ...
- Android开发中,那些让您觉得相见恨晚的方法、类或接口
Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...
随机推荐
- day11---异步IO\数据库\队列\缓存
一.RabbitMQ队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息 ...
- 解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)
解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-sourc ...
- 解剖SQLSERVER 第四篇 OrcaMDF里对dates类型数据的解析(译)
解剖SQLSERVER 第四篇 OrcaMDF里对dates类型数据的解析(译) http://improve.dk/parsing-dates-in-orcamdf/ 在SQLSERVER里面有几 ...
- MicroPHP 2.2.0 发布
ver 2.2.0: 增加了: 1.$this->cache为最新的phpfastcache2.1,缓存功能更加强大,而且编写自己的缓存类非常容易. 2.自定义缓存类说明: ...
- 细心很重要---猜猜这个SQL执行的什么意思
今天在帮客户做语句优化的时候,突然遇到这样一个语句,类似下面的例子(原语句是个update) 例子中使用AdventureWorks数据中的两个表. productID 是[Production].[ ...
- 详解c#迭代器
迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式.简单来说,迭代器模式使得你能够获取到序列中的所有元素 ...
- java提高篇(三)-----java的四舍五入
Java小事非小事!!!!!!!!!!!! 四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了.在讲解之间我们先看如下一个经典的案例: public stat ...
- COM思想的背后
最近看公司的一些新产品和框架 , 发现清一色的“COM思想架构 ”, 这里说的“COM思想架构”是指不完全是标准COM组件的方式,而是指在设计上用到了COM思想. COM组件技术大概在1993年产生, ...
- [ZigBee] 6、ZigBee基础实验——定时器3和定时器4(8 位定时器)
上一节讲了16位定时器1,本节讲8位定时器3和定时器4! 1.综述 Timer 3 and Timer 4 are two 8-bit timers(8位定时器). Each timer has tw ...
- [ZigBee] 1、 ZigBee简介
前言 目前,中国大力推广的物联网是zigbee 应用的主战场,物联网通过智能感知.识别技术与普适计算(我还特意申请了个域名psjs.vip).泛在网络的融合应用,被称为继计算机.互联网之后世界信息产业 ...