Android 连接tomcat模拟登陆账号
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"> <TextView
android:text="请输入账号"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /> <EditText
android:id="@+id/username"
android:text="heyiyong"
android:layout_height="wrap_content"
android:layout_width="fill_parent" /> <TextView
android:text="请输入密码"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /> <EditText
android:text="123"
android:inputType="textPassword"
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_width="fill_parent" /> <Button
android:onClick="click"
android:text="登陆"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /> </LinearLayout>
package com.wuyou.submittoserver; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class MainActivity extends ActionBarActivity { private static final int OK = 200;
private EditText usernameEditText;
private EditText passwrodEditText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); usernameEditText = (EditText) findViewById(R.id.username);
passwrodEditText = (EditText) findViewById(R.id.password);
} public void click(View view) {
final String username = usernameEditText.getText().toString().trim();
final String password = passwrodEditText.getText().toString().trim();
//Android默认模拟器外部的地址为10.0.2.2,而不是localhost和127.0.0.1
final String serverPath = "http://10.0.2.2:8080/login.jsp";
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
//给出提示:账号密码不许为空
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
//使用GET方式请求服务器只能这样
URL url = new URL(serverPath + "?username=" + username + "&password=" + password);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");
int responseCode = httpURLConnection.getResponseCode();
if (200 == responseCode) {
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
final String responseMsg = bufferedReader.readLine();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_LONG).show();
}
});
} else {
System.out.println("responseCode = " + responseCode);
//连接服务器出错,错误代码为:responseCode 根据代码值告诉用户出错的原因
//....
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} }
}
不要忘记权限:
<uses-permission android:name="android.permission.INTERNET"/>
jsp页面为:
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username = " + username);
System.out.println("password = " + password);
if("heyiyong".equals(username) && "123".equals(password)) {
response.getOutputStream().write("login successful!".getBytes());
} else {
response.getOutputStream().write("wrong username or password.".getBytes());
}
%>
Android 连接tomcat模拟登陆账号的更多相关文章
- NetworkComms V3 模拟登陆
演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...
- 新浪微博模拟登陆+数据抓取(java实现)
模拟登陆部分实现: package token.exe; import java.math.BigInteger; import java.util.Random; import org.apache ...
- 使用OKHttp模拟登陆知乎,兼谈OKHttp中Cookie的使用!
本文主要是想和大家探讨技术,让大家学会Cookie的使用,切勿做违法之事! 很多Android初学者在刚开始学习的时候,或多或少都想自己搞个应用出来,把自己学的十八般武艺全都用在这个APP上,其实这个 ...
- HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息
前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...
- 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)
urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...
- Python模拟登陆某网教师教育网
本文转载自看雪论坛[作者]rdsnow 不得不说,最近的 Python 蛮火的,我也稍稍了解了下,并试着用 Python 爬取网站上的数据 不过有些数据是要登陆后才能获取的,我们每年都要到某教师教育网 ...
- Scrapy模拟登陆豆瓣抓取数据
scrapy startproject douban 其中douban是我们的项目名称 2创建爬虫文件 进入到douban 然后创建爬虫文件 scrapy genspider dou douban. ...
- 爬虫之 cookie , 验证码,模拟登陆,线程
需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...
- 使用selenium模拟登陆淘宝、新浪和知乎
如果直接使用selenium访问淘宝.新浪和知乎这些网址.一般会识别出这是自动化测试工具,会有反制措施.当开启开发者模式后,就可以绕过他们的检测啦.(不行的,哭笑) 如果网站只是对windows.na ...
随机推荐
- c语言,strcmp(),字符串比较,看Asic 码,str1>str2,返回值 > 0;两串相等,返回
#include<stdio.h> #include<string.h> int main() { char *buffer1="aaa",*buffer ...
- uiscrollview上的 uipangesturerecognizer冲突
最近在tableview里的cell imageview加了个 uipangesturerecognizer发现优先滚动imageview,往上拖的时候,tableView不响应滚动了,原来是tabl ...
- 颜色空间转换 cvtColor()[OpenCV 笔记13]
void cvtColor(InputArray src, OutputArray dst, ) src: 输入图像 dst: 输出图像 code: 颜色空间转换标识符 OpenCV2的CV_前缀宏命 ...
- 关于C++对汉字拼音的处理
直到目前我没有找到比较合适的输入汉字输出拼音的函数,那么根据网上流传的几个源码进行了改编,写成了输入汉字输出拼音的函数.对于此函数不能说强大,但是至少稳定可用,输出结果还没有发现什么错误. 那么下面我 ...
- 【BZOJ1042】【DP + 容斥】[HAOI2008]硬币购物
Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...
- 在阿里云服务器ubuntu14.04运行netcore
从netcore1.0正式发布就很激动,想要赶紧学习. 最近博客园的一篇文章给了完整的指导非常感谢,但是在实际实现到发布到阿里云服务器遇到一些问题,记录下来. 首先上基础文章http://www.cn ...
- MySQL配置文件详解
MYSQL 配置文件详解 “全局缓存”.“线程缓存”,全局缓存是所有线程共享,线程缓存是每个线程连接上数据时创建一个线程(如果没有设置线程池),假如有200连接.那就是200个线程,如果参数设定值是1 ...
- linux强大IDE——Geany配置说明
今天开始用Ubuntu了(主要是为了防止自己在windows下不自觉的打游戏之类的) 刚开始用的很不习惯 找不到合适的编译器(DEV c++什么时候才能出Linux的啊) 先后下了codeli ...
- iOS面试题6.30总结
越来越多的人投入iOS这个行业中,但是作为刚才学校毕业的学生,我们没有任何经验.或者经验很少.但是这也不能阻挡我们对苹果的热情,想投入iOS的开发中.而作为进入企业的第一步,我们要参加面试.面试中我们 ...
- jQuery选择器(适合初学者哟....)
选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理解,它们本身用法就非常简单,我更希望的是它能够提升个人编写 ...