最近在做一个小项目,项目开发中需要实现一个登录验证功能,具体的要求就是,在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. 关于PHP堆栈与列队

    在PHP中数组常被当作堆栈(后进先出:LIFO)与队列(先进先出:FIFO)结构来使用.PHP提供了一组函数可以用于push与pop(堆栈)还有shift与unshift(队列)来操作数组元素.堆栈与 ...

  2. xshell传输文件到Centos

    Reference: [1] http://www.myhack58.com/Article/sort099/sort0102/2015/61154.htm [2] http://www.cnblog ...

  3. jquery datepicker 只显示年月

    首先修改默认日期赋值 <script type="text/javascript"> $(function(){ $('#searchDate').datepicker ...

  4. Bookstore project using XAMPP 详细配置 Part 2

    2. Implementation of MySQL database in “phpMyAdmin” 1) Create database named “tbl_book” 2) I changed ...

  5. 跳转页面,传递参数——android

    android 跳转页面并传递对象(实体类)——项目中是集港收货类 网上资料:两种传递方法Serializable,parcelable 优劣比较:Serializable数据更持久化,网络传输或数据 ...

  6. ios的网络数据下载和json解析

    ios的网络数据下载和json解析 简介 在本文中,笔者将要给大家介绍如何使用nsurlconnection 从网上下载数据,以及解析json数据格式,以及如何显示数据和图片的异步下载显示. 涉及的知 ...

  7. .net core 中环境变量的配置

    配置文件: Properties目录下的launchSettings.json IISExpress配置 "ASPNET_ENV": "EnvironmentOne&qu ...

  8. PostgreSQL基础整理(二)

    存储过程 实现功能:针对工资表30岁以下,工资提升10% 30至40提升20% 40以上提升30% + 奖金(入参)返回平均薪酬 创建表: DROP TABLE emps; CREATE TABLE ...

  9. C#可扩展编程之MEF学习笔记(五):MEF高级进阶

    好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...

  10. 探求网页同步提交、ajax和comet不为人知的秘密(中篇)

    深入研究某项技术,了解使用这些技术的细节,其实最终目的都是为了完成一个选择问题:当我们要使用这些技术解决某个具体的问题时候我们到底该如何去选择.如果碰到有两种技术可以让我们达到同样的目的,我们就会不自 ...