本篇基于上一篇搭建的服务器端环境,具体介绍Android真机上传数据到tomcat服务器的交互过程
场景:Android客户端上传用户名和密码到tomcat服务端,tomcat服务器自动接收Android客户端上传的数据,并打印出结果
一、tomcat服务器端实现
1.首先启动tomcat服务器
直接点击“finish”即可,启动后效果如下:
2. 编写servlet实现:ServletDemo1.java
package com.servlet.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo1
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDemo1" })
public class ServletDemo1 extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Log Log = LogFactory.getLog(ServletDemo1.class);
/**
* 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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
// 获取请求的数据,并向控制台输出
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("-----> doGet username:" + username + " password:" + password);
}
/**
* 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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
// 获取请求的数据,并向控制台输出
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("-----> doPost username:" + username + " password:" + password);
}
}
3.运行Servlet
运行成功:
二、Android客户端实现
1. 主页 MainActivity
package com.example.client;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText username;
private EditText password;
private Button signup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.account);
password = (EditText) findViewById(R.id.password);
signup = (Button) findViewById(R.id.btnSign);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLogin();
}
});
}
// 发起HTTP请求
public void onLogin() {
new HttpTread(url, username.getText().toString(), password.getText().toString()).start();
}
}
说明:
2. First:是web工程名
3. test/ServletDemo1:是如下高亮部分映射的名称
2. 线程HttpTread
package com.example.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HttpTread extends Thread {
String url;
String username;
String password;
public HttpTread(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
private void send() throws IOException {
// 将username和password传给Tomcat服务器
url = url + "?username=" + username + "&password=" + password;
try {
URL httpUrl = new URL(url);
// 获取网络连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
// 设置请求方法为GET方法
conn.setRequestMethod("GET"); // 或 "POST"
// 设置访问超时时间
conn.setReadTimeout(5000);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
StringBuffer sb = new StringBuffer();
// 读取服务器返回的信息
while ((str = reader.readLine()) != null) {
sb.append(str);
}
// 把服务端返回的数据打印出来
System.out.println("HttpTreadResult:" + sb.toString());
} catch (MalformedURLException e) {
}
}
@Override
public void run() {
super.run();
try {
send();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
package="com.example.client"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="25"
android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
说明:
需要声明权限:<uses-permission android:name="android.permission.INTERNET" />
编码结束。
4. Android 客户端主页面:
填写用户名和密码,例如:用户名:aaaa 密码:bbb123
点击“提交”按钮。切换到Eclipse中,可以看到Tomcat自动打印出所提交的数据:
完成。
- Android 本地tomcat服务器接收处理手机上传的数据之环境搭建
上一篇:Android 使用tomcat搭建HTTP文件下载服务器 本篇文章 环境:win7 + jdk1.7 + tomcat v8.0.53 工具: 1.Eclipse Eclips ...
- 利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题
html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...
- (2)MyEclipse怎么关联本地Tomcat服务器
1,在MyEclipse中点击服务器按钮: 2,选择“Configure Server” 3,在弹出面板中选择 [Servers]-[Tomcat]-[对应版本的服务器] 5,看上图,先选择Enabl ...
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...
- 转 Android网络编程之使用HttpClient批量上传文件 MultipartEntityBuilder
请尊重他人的劳动成果,转载请注明出处:Android网络编程之使用HttpClient批量上传文件 http://www.tuicool.com/articles/Y7reYb 我曾在<Andr ...
- 解决ios手机上传竖拍照片旋转90度问题
html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...
- 利用exif.js解决手机上传竖拍照片旋转90\180\270度问题
原文:https://blog.csdn.net/linlzk/article/details/48652635/ html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针 ...
- android中的文件(图片)上传
android中的文件(图片)上传其实没什么复杂的,主要是对 multipart/form-data 协议要有所了解. 关于 multipart/form-data 协议,在 RFC文档中有详细的描述 ...
- 学习Git的一点心得以及如何把本地修改、删除的代码上传到github中
一:学习Github的资料如下:https://git.oschina.net/progit/ 这是一个学习Git的中文网站,如果诸位能够静下心来阅读,不要求阅读太多,只需要阅读前三章,就可以掌握Gi ...
随机推荐
- C# 校验Email(电子邮件)地址是否合法
用于校验给定的Email地址是否合法,只针对用于提供的Email地址的格式,不对其是否真实存在进行校验. /// <summary> /// 验证EMail是否合法 /// </su ...
- Ruby gem: Mac 系统下的安装与更新
官方链接:https://rubygems.org/pages/download#formats 下载安装: 1.点击上面链接进入到官网,从顶部的链接下载压缩包: 2.解压缩到指定文件夹,并通过 “c ...
- 如何解析oracle执行计划
要执行任何SQL语句,Oracle 必须推导出一个“执行计划”.查询的执行计划是 Oracle 将如何实现数据的检索,以满足给定 SQL 语句的描述.它只不过是其中包含的步骤及它们之间关系的顺序树.执 ...
- Genymotion模拟器无法开启的解决方法——Unable to start the virtual device,The virtual device got no IP address
前言 最近重装了电脑的系统,由win7换成了win8.1.android开发环境也重新配置了一遍.其他的都还好,就是genymotion模拟器一直开启失败. 自己尝试了很多方法,比如卸载重装软件,重新 ...
- level 6 - unit3 -- 非限制性定语从句
非限制性定语从句 例子1 he has a son who is a fireman who 引导一个定语从句. who 是修饰前面的son. 翻译的意思:他有一个消防员的儿子 he has a so ...
- win8 关闭防火墙
http://jingyan.baidu.com/article/b87fe19eddb4da5218356894.html
- lua封装的位运算
1.移位运算基础 --与 同为1,则为1 --或 有一个为1,则为1 --非 true为 false,其余为true --异或 相同为0,不同为1 --ZZMathBit = {} function ...
- hive 表优化
一.外部表和内部表的区别 (1)创建表时指定external关键字,就是外部表,不指定external就是内部表 (2)内部表删除后把元数据和数据都删除了,外部表删除后只是删除了元数据,不会删除hdf ...
- Android开发学习笔记-splash画面的显示
贴代码: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&qu ...
- 微信支付id出现的重复支付解决方法和app应用中多种支付方式之间的对比
1.微信支付的transId发起支付请求,未登录微信,先帐号登陆,否则直接去支付.这样的话,该transId跟该帐号绑定起来了, 如果下一次再重新使用该transId来支付请求,但是想切换其他的微信帐 ...