本篇基于上一篇搭建的服务器端环境,具体介绍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 ...
随机推荐
- Windows 7运行命令大全
Windows 7运行命令大全,小编整理了常用的45个Win7运行命令分享给大家:1.cleanmgr:打开磁盘清理工具 2.compmgmt.msc:计算机管理 3.conf:启动系统配置实用程序 ...
- Fragment管理工具类
Fragment相关→FragmentUtils.java→Demo addFragment : 新增fragment removeFragment : 移除fragment replaceFragm ...
- curl tutorial with examples of usage
原文:http://www.yilmazhuseyin.com/blog/dev/curl-tutorial-examples-usage/ 阮一峰的这个教程也不错:http://www.ruanyi ...
- MySQL死锁分析
死锁问题背景 做MySQL代码的深入分析也有些年头了,再加上自己10年左右的数据库内核研发经验,自认为对于MySQL/InnoDB的加锁实现了如指掌,正因如此,前段时间,还专门写了一篇洋洋洒洒的文章, ...
- JWT全面解读、使用步骤
JWT全面解读JWT全面解读前言JWT基本使用在pom.xml引入java-jwt示例如下概念介绍JWT消息构成头部playload标准中注册的声明 (建议但不强制使用)自定义数据签名signatur ...
- SSM框架面试题及答案整理
一.Spring面试题 1.Spring 在ssm中起什么作用? Spring:轻量级框架 作用:Bean工厂,用来管理Bean的生命周期和框架集成. 两大核心:①. IOC/DI(控制反转/依赖注入 ...
- jQuery form的load函数与el表达式赋值的冲突问题
问题: 在使用el表达式给表单中的项赋初始值的时候,总是失败,物流公司没有自动选中,物流单号也没有显示值. <form id="form" method="post ...
- 关于测试中哪些信息需要放到jira上面
1.每个新需求的功能点,全部在jira上一一呈现 2.每个bug也一样在jira上一一呈现 3.任务一个需要优化改进的点也一一在jira上呈现 然后程序员一直开发新功能和修改新bug,测试人员负责bu ...
- 关于解决emoji表情的存储
近段时间处理,由于工作需求,需要使得用户插入的emoji表情能够正常显示及使用,所以做个总结,以备后用. 说明:本方法只在mysql环境中测试 1.首先程序在连接数据库时,要指定数据库字符集的设置 c ...
- AES五种加密模式
分组密码在加密时明文分组的长度是固定的,而实用中待加密消息的数据量是不定的,数据格式可能是多种多样的.为了能在各种应用场合安全地使用分组密码,通常对不同的使用目的运用不同的工作模式. 一.电码本模式( ...