<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模拟登陆账号的更多相关文章

  1. NetworkComms V3 模拟登陆

    演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...

  2. 新浪微博模拟登陆+数据抓取(java实现)

    模拟登陆部分实现: package token.exe; import java.math.BigInteger; import java.util.Random; import org.apache ...

  3. 使用OKHttp模拟登陆知乎,兼谈OKHttp中Cookie的使用!

    本文主要是想和大家探讨技术,让大家学会Cookie的使用,切勿做违法之事! 很多Android初学者在刚开始学习的时候,或多或少都想自己搞个应用出来,把自己学的十八般武艺全都用在这个APP上,其实这个 ...

  4. HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息

    前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...

  5. 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)

    urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...

  6. Python模拟登陆某网教师教育网

    本文转载自看雪论坛[作者]rdsnow 不得不说,最近的 Python 蛮火的,我也稍稍了解了下,并试着用 Python 爬取网站上的数据 不过有些数据是要登陆后才能获取的,我们每年都要到某教师教育网 ...

  7. Scrapy模拟登陆豆瓣抓取数据

    scrapy  startproject douban 其中douban是我们的项目名称 2创建爬虫文件 进入到douban 然后创建爬虫文件 scrapy genspider dou douban. ...

  8. 爬虫之 cookie , 验证码,模拟登陆,线程

    需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...

  9. 使用selenium模拟登陆淘宝、新浪和知乎

    如果直接使用selenium访问淘宝.新浪和知乎这些网址.一般会识别出这是自动化测试工具,会有反制措施.当开启开发者模式后,就可以绕过他们的检测啦.(不行的,哭笑) 如果网站只是对windows.na ...

随机推荐

  1. c语言,strcmpi(),将一个串中的一部分与另一个串比较, 不管大小写

    #include<stdio.h> #include<string.h> 函数名: strncmpi 功 能: 将一个串中的一部分与另一个串比较, 不管大小写 用 法: int ...

  2. Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'

    出现这个问题,说明oracle的驱动包没有成功加载. 1.检查maven pom.xml有没有引入驱动:        <dependency>             <group ...

  3. 让你一分钟认识电子身份验证系统EID

    什么是EID eID是英文"Electronic Identity"的英文简称,中文名为"电子身份证"或"网络电子身份证",由公安部第三研究 ...

  4. mysql自增

    主键设置自增,同时主键需要是int类型

  5. MyEclipse2014中项目名更改后如何使用新的项目名部署到Tomcat中去

    在项目中调试的时候突然发现我复制的项目(项目名修改过了)部署在Tomcat中运行的时候还是显示的是原来的项目名,以至于我使用新的项目名称作为URL请求竟然是404,我去,当时感觉就不怎么好了. 当然, ...

  6. iOS 设置图片imageView圆角——对图片进行裁剪

    以前设置图片圆角总是把imageView设置成圆形,然后设置maskToBounds为YES,其实这样处理很消耗性能,图片多了之后比较卡,最好将图片进行裁剪后显示:这里有个分类可以用: UIImage ...

  7. 你喜欢使用eclipse+tomcat编程吗?!

    在eclipse中配置tomcat简直是一场灾难! 开源软件确实给开发者极大的自由,但,同时也带来额外的麻烦. eclipse+tomcat只是其中之一.我有幸碰到了N次,在N+1次时,不得不提笔撰文 ...

  8. 下载youku视频(python3)

    https://github.com/chenfengyuan/download-youku-video 用tornado写的下载脚本, 从flvcd.com得到下载地址. 因为我这边连youku的速 ...

  9. mybatis()

    ---------------------------------mysql分页---------------------------------- public void selectList(in ...

  10. Cron运行原理

    from:http://blog.chinaunix.net/uid-20682147-id-4977039.html 目录 目录 1 1. 前言 1 2. 示例 1 3. 工作过程 2 4. 一个诡 ...