j2me与jsp通信技巧

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

最近在开发j2me与jsp通信传多参数时有点心得,也因网上这类资料比较少所以写下来与大家分享,写得不好之处请大家多指教!

首先当然是要用以下三个类了:

HttpConnection conn = null;//用于连接到web服务

InputStream input = null;//用于接收返回信息

DataOutputStream output = null;//用于发送数据(当然也可以用OutputStream,只是DataOutputStream有更多实用的方法)

然后就是用conn = (HttpConnection)Connector.open(url)方法来建立连接

url是String类型的如

String url="http: //202.103.191.61:80/test.jsp";

string url2="http: //www.express.com/test.jsp";

如果是用ip地址作为参数一定要加上端口号,用网址可不用默认就是80嘛!

接着设置web服务接收的一些参数

conn.setRequestMethod(HttpConnection.POST);//也可以用get

conn.setRequestProperty("IF-Modified-Since", "29 May 2004 15:17:19 GMT");

conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");

conn.setRequestProperty("Content-Language", "en-CA");

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

打开输出流,传数据

output = c.openDataOutputStream();

output.writeUTF("&test1="+test1);

output.writeUTF("&test2="+test2);

output.writeUTF("&test3="+test3);

output.writeUTF("&test4="+test4);

output.flush();

到这里实际上就是我们在浏览器中输入http//202.103.191.61:80/test.jsp&test1=test1&test2=test2&test3=test3&test4=test4

注意到没有test.jsp后面全是 &参数名=值 第一个不是以?开头(但如果参数是只有一个或两个时可以不第一个不用&都行,不知道为什么)!

然后就是取得返回信息了,

input = c.openDataInputStream();

int ch;

StringBuffer b=new StringBuffer;

while ((ch = is.read()) != -1) {

b.append((char) ch);

System.out.println(b);

}

最后别忘闭流!

jsp程序里就是用request.getParameter();来取数据,然后进行处理啦,就不多说了!

附j2me源码

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import java.io.*;

public class SendTestMidlet extends MIDlet implements CommandListener {

Display display = null;

TextField ttest1,ttest2,ttest3,ttest4;

Form form;

String url = "http://202.103.191.61:80/test.jsp";

static final Command send = new Command("注册", Command.OK, 2);

static final Command exit = new Command("退出", Command.EXIT, 2);

String myname,pas1,pas2,test4;

public SendTestMidlet(){

display = Display.getDisplay(this);

ttest1 = new TextField("Name:", " ", 25, TextField.ANY);

ttest2 =new TextField("password:"," ",25,TextField.ANY);

ttest3 =new TextField("password2:"," ",25,TextField.ANY);

ttest4 =new TextField("note:"," ",25,TextField.ANY);

form = new Form("注册信息");

}

public void startApp() throws MIDletStateChangeException {

form.append(ttest1);

form.append(ttest2);

form.append(ttest3);

form.append(ttest4);

form.addCommand(send);

form.addCommand(exit);

form.setCommandListener(this);

display.setCurrent(form);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

notifyDestroyed();

}

public void sendData(String url) throws IOException {

HttpConnection conn = null;

InputStream input = null;

DataOutputStream output = null;

StringBuffer b = new StringBuffer();

TextBox t = null;

try {

conn = (HttpConnection)Connector.open(url);

conn.setRequestMethod(HttpConnection.POST);

conn.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");

conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");

conn.setRequestProperty("Content-Language", "en-CA");

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

output = conn.openDataOutputStream();

output.writeUTF("&name="+myname);

output.writeUTF("&pas1="+pas1);

output.writeUTF("&pas2="+pas2);

output.writeUTF("&test4="+test4);//.getBytes());

output.flush();

input = conn.openDataInputStream();

int ch;

while ((ch = input.read()) != -1) {

b.append((char) ch);

System.out.print((char)ch);

}

t = new TextBox("Date", b.toString(), 1024, 0);

t.setCommandListener(this);

}

finally {

if(input!= null) {

input.close();

}

if(output != null) {

output.close();

}

if(conn != null) {

conn.close();

}

}

display.setCurrent(t);

}

public void commandAction(Command conn, Displayable d) {

String label = conn.getLabel();

if(label.equals("exit")) {

destroyApp(true);

} else if (label.equals("date?")) {

myname = ttest1.getString();

pas1 =ttest2.getString();

pas2 =ttest3.getString();

test4 =ttest4.getString();

try {

sendData(url);

}catch(IOException e) {}

}

}

}

by dwinyu

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航