} catch (IOException e)
{
e.printStackTrace();
}
}
服务器端收到客户端传送过来的Stream后,处理起来更简单,调用Account.deserialize(dis)就可以得到account对象了。
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
int length = request.getContentLength();
System.out.println(length);
DataInputStream dis = new DataInputStream(request.getInputStream());
Account myAccount = Account.deserialize(dis);
System.out.println(myAccount.toString());
}
我们下面做个简单的MIDlet,目的是收集用户填写的注册信息然后发送给服务器。界面如下所示:
代码如下所示:
package com.j2medev.mingjava;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import java.io.*;
import javax.microedition.io.*;
public class NetworkMIDlet extends MIDlet implements CommandListener
{
private Display display;
private Form mainForm;
private TextField userName;
private TextField email;
private TextField age;
private ChoiceGroup gender;
private NetworkThread nt;
public static final Command connectCommand = new Command("Connect",
Command.ITEM, 2);
public static final Command exitCommand = new Command("Exit", Command.EXIT,
1);
public static final String URL = "http://localhost:8088/net/myservlet";
protected void startApp() throws MIDletStateChangeException
{
initMIDlet();
}
private void initMIDlet()
{
display = Display.getDisplay(this);
mainForm = new Form("个人信息");
userName = new TextField("姓名",null,20,TextField.ANY);
email = new TextField("电子信箱",null,25,TextField.EMAILADDR);
age = new TextField("年龄",null,20,TextField.ANY);
gender = new ChoiceGroup("性别",Choice.EXCLUSIVE);
gender.append("男",null);
gender.append("女",null);
mainForm.append(userName);
mainForm.append(email);
mainForm.append(age);
mainForm.append(gender);
mainForm.addCommand(connectCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
nt = new NetworkThread(this);
nt.start();
}