J2ME与JSP实现通信首先当然是要用以下三个类了:
- HttpConnectionconn=null;//用于连接到web服务
- InputStreaminput=null;//用于接收返回信息
- DataOutputStreamoutput=null;//用于发送数据
(当然也可以用OutputStream,只是DataOutputStream有更多实用的方法)然后就是用conn=(HttpConnection)Connector.open(url)方法来建立连接
url是String类型的如
- Stringurl="http://202.103.191.61:80/test.jsp";
- stringurl2="http://www.express.com/test.jsp";
如果是用ip地址作为参数一定要加上端口号,用网址可不用默认就是80嘛!
接着设置web服务接收的一些参数
- conn.setRequestMethod(HttpConnection.POST);//也可以用get
- conn.setRequestProperty("IF-Modified-Since","29May200415:17:19GMT");
- conn.setRequestProperty("User-Agent","Profile/MIDP-1.0Configuration/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();
- intch;
- StringBufferb=newStringBuffer;
- while((ch=is.read())!=-1){
- b.append((char)ch);
- System.out.println(b);
- }
最后别忘闭流!
JSP程序里就是用request.getParameter();来取数据,然后进行处理啦,就不多说了!
附J2ME与JSP实现通信源码
- importjavax.microedition.lcdui.*;
- importjavax.microedition.midlet.*;
- importjavax.microedition.io.*;
- importjava.io.*;
- publicclassSendTestMidletextendsMIDletimplementsCommandListener{
- Displaydisplay=null;
- TextFieldttest1,ttest2,ttest3,ttest4;
- Formform;
- Stringurl="http://202.103.191.61:80/test.jsp";
- staticfinalCommandsend=newCommand("注册",Command.OK,2);
- staticfinalCommandexit=newCommand("退出",Command.EXIT,2);
- Stringmyname,pas1,pas2,test4;
- publicSendTestMidlet(){
- display=Display.getDisplay(this);
- ttest1=newTextField("Name:","",25,TextField.ANY);
- ttest2=newTextField("password:","",25,TextField.ANY);
- ttest3=newTextField("password2:","",25,TextField.ANY);
- ttest4=newTextField("note:","",25,TextField.ANY);
- form=newForm("注册信息");
- }
- publicvoidstartApp()throwsMIDletStateChangeException{
- form.append(ttest1);
- form.append(ttest2);
- form.append(ttest3);
- form.append(ttest4);
- form.addCommand(send);
- form.addCommand(exit);
- form.setCommandListener(this);
- display.setCurrent(form);
- }
- publicvoidpauseApp(){
- }
- publicvoiddestroyApp(booleanunconditional){
- notifyDestroyed();
- }
- publicvoidsendData(Stringurl)throwsIOException{
- HttpConnectionconn=null;
- InputStreaminput=null;
- DataOutputStreamoutput=null;
- StringBufferb=newStringBuffer();
- TextBoxt=null;
- try{
- conn=(HttpConnection)Connector.open(url);
- conn.setRequestMethod(HttpConnection.POST);
- conn.setRequestProperty("IF-Modified-Since","29Dec200115:17:19GMT");
- conn.setRequestProperty("User-Agent","Profile/MIDP-1.0Configuration/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();
- intch;
- while((ch=input.read())!=-1){
- b.append((char)ch);
- System.out.print((char)ch);
- }
- t=newTextBox("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);
- }
- publicvoidcommandAction(Commandconn,Displayabled){
- Stringlabel=conn.getLabel();
- if(label.equals("exit")){
- destroyApp(true);
- }elseif(label.equals("date?")){
- myname=ttest1.getString();
- pas1=ttest2.getString();
- pas2=ttest3.getString();
- test4=ttest4.getString();
- try{
- sendData(url);
- }catch(IOExceptione){}
- }
- }
- }