安卓向服务器发送List数据
第一步:
首先写一个自定义的JavaBean,以UserInfo.java为例,需要实现对象序列化的接口,因为之后输出流对象需要实现输出可序列化的对象。不这样的话,后续时发送时会报异常
package xl.java.bean;
import java.io.Serializable;
/**
* 用户信息
* @author xl 2012-9-20
*/
public class UserInfo implements Serializable
{
private static final long serialVersionUID = 1L;
/**
* 用户名
*/
private String UserName;
/**
* 密码
*/
private String Password;
/**
* 昵称
*/
private String NickName;
/**
* QQ号
*/
private int QQNumber;
/**
* 电话号
*/
private String TelNumber;
/**
* 年龄
*/
private int Age;
public String getUserName()
{
return UserName;
}
public void setUserName(String userName)
{
UserName = userName;
}
public String getPassword()
{
return Password;
}
public void setPassword(String password)
{
Password = password;
}
public String getNickName()
{
return NickName;
}
public void setNickName(String nickName)
{
NickName = nickName;
}
public int getQQNumber()
{
return QQNumber;
}
public void setQQNumber(int qQNumber)
{
QQNumber = qQNumber;
}
public String getTelNumber()
{
return TelNumber;
}
public void setTelNumber(String telNumber)
{
TelNumber = telNumber;
}
public int getAge()
{
return Age;
}
public void setAge(int age)
{
Age = age;
}
}
注意,服务器与客户端的javabean包名必须一致,不然的话会报ClassNotFoundException异常。
第二步:
编写客户端模拟发送数据的类SendData.java。中间一大段的连接处理,具体解释可参考:
JDK中的URLConnection参数详解 - wlzf6296149的专栏 - 博客频道 - CSDN.NET
package xl.java.send;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import xl.java.bean.UserInfo;
/**
* 模拟发送数据
* @author xl 2012-9-20
*
*/
public class SendData
{
private static final String BASIC_URL_QUEST =
"http://192.168.1.1:8080/test/TestServlet";
public static void main(String[] args)
{
SendData senddata=new SendData();
try
{
senddata.sendDataToServer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 上传处理结果
*
* @throws IOException
*
*/
private void sendDataToServer() throws IOException
{
//用于servlet判别请求,执行相应方法
String QuestId = "SubmitUserInfoList";
//模拟发送自定义类型的List数据
List<UserInfo> listdata = new ArrayList<UserInfo>();
for (int i = 0; i < 10; i++)
{
UserInfo li = new UserInfo();
li.setUserName("XL" + i);
li.setPassword("00000" + i);
li.setQQNumber(1234567 + i);
li.setTelNumber("15012344321" + i);
li.setNickName("xiaolang" + i);
li.setAge(18 + i);
listdata.add(li);
}
URL url = new URL(BASIC_URL_QUEST);
try
{
URLConnection con = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) con;
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setRequestProperty("Content-type",
"application/x-java-serialized-object");
//不设置这个默认为Get,服务器会没反应,不知道什么情况,
//纠结了很久,改成Post的话,servlet里的
//doPost方法就有反应了
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.connect();
OutputStream outStrm = httpUrlConnection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outStrm);
//输出流第一段数据是QuestId的值
oos.writeObject(QuestId);
//第二段数据是List数据
oos.writeObject(listdata); oos.flush();
oos.close();
InputStream inStrm = httpUrlConnection.getInputStream();
System.out.println("数据发送成功!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
编写servlet,接收数据
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import xl.java.bean.UserInfo;
/**
* @author xl 2012-9-20
*
*/
public class TestServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private Connection mConnection = null;
private Statement mStatement = null;
private String QuestId = "";
private static final String SUBMIT_USERINFO_LIST = "SubmitUserInfoList";// 客户端提交到用户信息
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
{
System.out.println("________---------doPost--------_____________");
try
{
// 链接数据库
Class.forName("org.gjt.mm.<a href="http://lib.csdn.net/base/14" class="replace_word" title="undefined" target="_blank" style="color: rgb(223, 52, 52); font-weight: bold;">mysql</a>.Driver").newInstance();
mConnection =
DriverManager
.getConnection("jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=UTF-8");
mStatement =
mConnection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// 如果不是通过URL的Get形式上传数据时,调用此方法,获取上传的list数据
getListDataByObjectInputStream(request, response);
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 获取输入流中的数据
*
* @param request
* @param response
* @throws IOException
* @throws ClassNotFoundException
*/
private void getListDataByObjectInputStream(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ClassNotFoundException
{
System.out.println("---------getListDataByObjectInputStream--------");
response.setContentType("text/html");
InputStream inStream = request.getInputStream();
ObjectInputStream objInStream = new ObjectInputStream(inStream);
QuestId = (String) objInStream.readObject();
@SuppressWarnings("unchecked")
List<UserInfo> inList = (List<UserInfo>) objInStream.readObject();
if (QuestId.equals(SUBMIT_USERINFO_LIST))
{
System.out.println("QuestId.equals(SUBMIT_ORDER_LIST)");
submitOrderList(request, response, inList);
}
objInStream.close();
System.out.println("objInStream.close()");
}
/**
* @param request
* @param response
* @param inList
*/
private void submitOrderList(HttpServletRequest request,
HttpServletResponse response, List<UserInfo> inList)
{
// 获取数据,插入数据库
for (UserInfo item : inList)
{
System.out.println("UserName=" + item.getUserName());
System.out.println("Password=" + item.getPassword());
System.out.println("NickName=" + item.getNickName());
System.out.println("QQNumber=" + item.getQQNumber());
System.out.println("TelNumber=" + item.getTelNumber());
System.out.println("Age=" + item.getAge() + "\n");
}
/**
* 插入数据库代码可以写在这..
*/
}
}
最后:
运行SendData.java文件,可看到控制台输出,根据前面的设定我们知道,客户端和服务端已经建立连接,并且数据成功发送给了服务端
参考链接:
安卓向服务器发送List数据的更多相关文章
- libcurl HTTP POST请求向服务器发送json数据【转】
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- libcurl HTTP POST请求向服务器发送json数据
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- curl向web服务器发送json数据
c++使用libcurl: /* *g++ demo.cpp -g -Wall -lcurl */ #include <string.h> #include <stdlib.h> ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- 【转】iOS开发网络篇—发送json数据给服务器以及多值参数
原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...
- ActionScript接收socket服务器发送来的数据
原文地址:http://www.asp119.com/news/2009522181815_1.htm 从socket中接收数据的方法取决于你使用socket类型,Socket和XMLSocket都可 ...
- ajax-向服务器发送请求
ajax-向服务器发送请求 1.将请求发送到服务器,使用XMLHttpRequest对象的 open() 和 send() 方法. xmlhttp. open(method,url,async ...
- AJAX向服务器发送请求
使用 XMLHttpRequest 对象的 open() 和 send() 方法: 方法 描述 open(method,url,async) 规定请求的类型.URL 以及是否异步处理请求. metho ...
- 介绍一款chrom浏览器插件 DHC是一款使用chrome模拟REST客户端向服务器发送测试数据的谷歌浏览器插件
先打个小广告哈 公司招java架构师,月薪25K以上,负责电商平台架构工作,工作地点在北京 1号线永安里站 附近,如有意向 请把简历发我邮箱jia6235@163.com 可以内部推荐. DHC是一款 ...
随机推荐
- 车牌号对应归属地及城市JSON带简码
车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...
- 初试visual studio2012的新型数据库LocalDB
http://www.cnblogs.com/zhangran/archive/2012/08/21/2649200.html 今天在vs2012里面打开以前的mvc3项目,结果弹出警告说在vs201 ...
- FluentData,它是一个轻量级框架,关注性能和易用性。
http://www.cnblogs.com/zengxiangzhan/p/3250105.html FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.M ...
- 深入了解A*
一.前言 在这里我将对A*算法的实际应用进行一定的探讨,并且举一个有关A*算法在最短路径搜索的例子.值得注意的是这里并不对A*的基本的概念作介绍,如果你还对A*算法不清楚的话,请看姊妹篇<初识A ...
- 旋转屏幕时,假如自定义的xib大小变了,可能是这个属性没有修改
虽然xib内部启用了自动布局,但是当xib放入外界,xib自身的autoresizing是存在的
- 我对 impress.js 源码的理解
源码看了两天,删掉了一些优化,和对 ipad 的支持,仅研究了其核心功能的实现,作以下记录. HTML 结构如下: <!doctype html> <html lang=" ...
- zabbix 邮件报错 Support for SMTP authentication was not compiled in
服务器系统是centos6.5 zabbix版本是3.0.4 根据 网上教程配置好邮件脚本后,触发发送邮件的时候报错: Support for SMTP authentication was not ...
- WebService中方法的相关注意事项
2014-11-14 在WebService中定义方法,有一些注意的地方: (1) 方法上面需要增加 [WebMethod] 属性,标志该方法是一个WebService方法: (2)方法的返回值可以为 ...
- DIV+CSS例子
DIV水平居中+垂直居中 #main_zone{ width:1190px; height:570px; background-color:#fff; margin:0 auto; /*左右居中*/ ...
- 高性能的分布式内存对象缓存系统Memcached
Memcached概述 什么是Memcached? 先看看下面几个概念: Memory:内存存储,不言而喻,速度快,对于内存的要求高,不指出的话所缓存的内容非持久化.对于CPU要求很低,所以常常采 ...