最近在开发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
