跟服务器交互的登录Demo
服务器写死 账号密码,演示登录
服务器代码:
开发工具MyEclipse
public class LoginServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//getParameter 方法中默认使用iso-8859-1对接收到的参数进行转码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 把乱码的字符串的字节打回原形,转成UTF-8
String newUsername = new String(username.getBytes("iso-8859-1"),"UTF-8");
String newPassword = new String(password.getBytes("iso-8859-1"),"UTF-8");
System.out.println("newUsername:"+newUsername+",newPassword:"+newPassword);
// tomcat默认使用iso-8859-1转码,但是iso-8859-1不支持中文,tomcat会查看操作系统的本地码表,本地字符集编码是gbk
if("123".equals(newUsername) && "abc".equals(newPassword)){
response.getOutputStream().write("login success".getBytes());
}else{
response.getOutputStream().write("login failture".getBytes());
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
客户端代码
开发工具:Android eclipse
public class MainActivity extends Activity {
private EditText et_qq;
private EditText et_pwd;
private String path = "http://192.168.7.171:8080/web/servlet/LoginServlet";
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
String result = (String) msg.obj;
Toast.makeText(MainActivity.this, result, 0).show();
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到两个输入框
et_qq = (EditText) findViewById(R.id.et_qq);
et_pwd = (EditText) findViewById(R.id.et_pwd);
}
public void login(View v){
// 以get方式向服务器端提交数据
final String qq = et_qq.getText().toString().trim();
final String pwd = et_pwd.getText().toString().trim();
if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)){
Toast.makeText(this, "qq号码和密码都不能为空", 0).show();
return;
}else{
new Thread(){
public void run() {
try {
// 1.创建一个URL对象,打开一个http类型的连接
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String data = "username="+qq+"&password="+pwd;
// 2.给连接设置请求参数
conn.setRequestMethod("POST");
conn.setConnectTimeout(3000); // 设置连接的超时时间
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length()+"");
//conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
// 以二进制流的形式写到服务器端;
// 设置是否允许把数据写到服务器端,true表示允许,false表示不允许
conn.setDoOutput(true);
conn.getOutputStream().write(data.getBytes());
// 3.得到服务器端返回的响应码是否为200,应该接收服务器端返回的二进制输入流
// 得到服务器端返回的响应码
int code = conn.getResponseCode();
if(code == 200){
//接收服务器端返回的二进制输入流
InputStream is = conn.getInputStream();
String result = StreamTools.readStream(is);
Message msg = Message.obtain();
msg.obj = result;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
}
StreamTool.java
public class StreamTools {
/**
* 把二进制数据转换成字符串
* @param is
* @return
*/
public static String readStream(InputStream is) {
String result = null;
// 创建一个输出流,用于写数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int len = -1;
byte[] buffer = new byte[1024];
while((len = is.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
result = baos.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
Xml
<LinearLayout 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"
android:orientation="vertical"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入qq号码"
android:id="@+id/et_qq" /> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:id="@+id/et_pwd" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="login"
/> </LinearLayout>
跟服务器交互的登录Demo的更多相关文章
- Android和FTP服务器交互,上传下载文件(实例demo)
今天同学说他备份了联系人的数据放在一个文件里,想把它存到服务器上,以便之后可以进行下载恢复..于是帮他写了个上传,下载文件的demo 主要是 跟FTP服务器打交道-因为这个东东有免费的可以身亲哈 1. ...
- 从高处理解android与服务器交互(看懂了做开发就会非常的容易)
今天帮一个朋友改一个bug 他可以算是初学者吧 .我给他看了看代码,从代码和跟他聊天能明显的发现他对客户端与服务器交互 基本 不是很了解.所以我花了更多时间去给他讲客户端与服务器的关系.我觉得从这个高 ...
- AngularJs与Java Web服务器交互
AngularJs是Google工程师研发的产品,它的强大之处不是几句话就能描述的,只有真正使用过的人才能体会到,笔者准备在这篇文章中,以一个简单的登录校验的例子说明如何使用AngularJs和Web ...
- Xamarin.Android再体验之简单的登录Demo
一.前言 在空闲之余,学学新东西 二.服务端的代码编写与部署 这里采取的方式是MVC+EF返回Json数据,(本来是想用Nancy来实现的,想想电脑太卡就不开多个虚拟机了,用用IIS部署也好) 主要是 ...
- 跟服务器交互的Web表单(form)
使用HTML来构建可以跟服务器交互的Web表单(form),通过给你的form元素添加一个action属性来达到此目的. action属性的值指定了表单提交到服务器的地址. 例如: <form ...
- html name id, 与服务器交互必须有name
html name id, 与服务器交互必须有name 在HTML中元素的ID和Name的区别和联系. 今天写了个测试,在php脚本里怎么也获取不到$_POST['userName']的值,经检查在h ...
- 易Android登录Demo
上一页介绍Android项目简单的页面跳转实例,算是对开发环境的熟悉,这一篇将在此基础上增加一些简单的逻辑,实现登录的效果. 登录之前: 登录成功: watermark/2/text/aHR0cDov ...
- 20171018 微信小程序客户端数据和服务器交互
-- 时常在想,怎么样才能把知识写的清晰,其实是我理解的不够清晰 微信小程序其实是一个客户端页面,也是需要和服务器交互才能体现数据. 1 --服务器搭建Web API :MVC4 中的一个模板, 如下 ...
- 浏览器与服务器交互原理以及用java模拟浏览器操作v
浏览器应用服务器JavaPHPApache * 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie, * 简单的讲,当浏 ...
随机推荐
- JAVA程序中SQL语句无法传递中文参数
vi /etc/my.cnf [mysqld]# The default character set that will be used when a new schema or table is# ...
- EChart数据的异步加载和更新
ECharts是国内开发一款图标插件,在网页中我们经常要用到图标显示,直接引用十分方便. 直接到ECharts主页调用插件 <!DOCTYPE html> <html style=& ...
- CSS中定义CLASS时,中间有空格和没空格的区别是什么?
.example .pp{ color: orange; } .example.pp2 { color: green; }如上面的两个定义一个是中间有空格,一个是中间没空格. 第一个class要这样写 ...
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- 关于ubuntu16无线网卡RTL8723BE频繁掉线及信号不足的解决办法
最近在新电脑上装了ubuntu16,结果wifi经常连不上,连上了过段时间就掉线,路由器就在电脑的旁边,而且信号非常的若. 但是windows系统没有任何问题,所以就在网上找解决办法,也按照网上的方法 ...
- gcc -Wall -pedantic -ansi(转载)
转载自R-G-Y-CQ的新浪博客 -Wall显示所有的警告信息 -Wall选项可以打开所有类型的语法警告,以便于确定程序源代码是否是正确的,并且尽可能实现可移植性. 对Linux开发人员来讲,GCC给 ...
- mysql 字符串 日期互转
一.字符串转日期 下面将讲述如何在MySQL中把一个字符串转换成日期: 背景:rq字段信息为:20100901 1.无需转换的: SELECT * FROM tairlist_day WHERE rq ...
- 前端项目通用、常用js common.js
var url = location.href; if (url.toLowerCase().indexOf("/akweb_admin/") == -1) { function ...
- ABP集合贴(转)
ABP集合贴 本文背景 公司最近规划的新框架准备基于ABP来搭建,自从在阳铭博客看到ABP框架的介绍后,就一直持续关注着,但还没真正在实际项目中直接使用ABP,只是自己做了一些学习和Demo.ABP所 ...
- 关于利用bat文件调用exe批量处理文件下的文件的问题
for %%i in (E:\radar_20120721\sjz_sa\*.bin) do start/wait radas.exe -i=%%i -o=E:\longjiang\out 找到 文件 ...