转载

浅谈J2ME与JSP实现通信技巧

J2ME与JSP实现通信首先当然是要用以下三个类了:

  1. HttpConnectionconn=null;//用于连接到web服务  
  2. InputStreaminput=null;//用于接收返回信息  
  3. DataOutputStreamoutput=null;//用于发送数据 

(当然也可以用OutputStream,只是DataOutputStream有更多实用的方法)然后就是用conn=(HttpConnection)Connector.open(url)方法来建立连接

url是String类型的如

  1. Stringurl="http://202.103.191.61:80/test.jsp";  
  2. stringurl2="http://www.express.com/test.jsp"

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

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

  1. conn.setRequestMethod(HttpConnection.POST);//也可以用get  
  2. conn.setRequestProperty("IF-Modified-Since","29May200415:17:19GMT");  
  3. conn.setRequestProperty("User-Agent","Profile/MIDP-1.0Configuration/CLDC-1.0");  
  4. conn.setRequestProperty("Content-Language","en-CA");  
  5. conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 

打开输出流,传数据

  1. output=c.openDataOutputStream();  
  2. output.writeUTF("&test1="+test1);  
  3. output.writeUTF("&test2="+test2);  
  4. output.writeUTF("&test3="+test3);  
  5. output.writeUTF("&test4="+test4);  
  6. output.flush(); 

到这里实际上就是我们在浏览器中输入http//202.103.191.61:80/test.jsp&test1=test1&test2=test2&test3=test3&test4=test4
注意到没有test.jsp后面全是&参数名=值第一个不是以?开头(但如果参数是只有一个或两个时可以不第一个不用&都行,不知道为什么)!

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

  1. input=c.openDataInputStream();  
  2. intch;  
  3. StringBufferb=newStringBuffer;  
  4. while((ch=is.read())!=-1){  
  5. b.append((char)ch);  
  6. System.out.println(b);  

最后别忘闭流!

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

附J2ME与JSP实现通信源码

  1. importjavax.microedition.lcdui.*;  
  2. importjavax.microedition.midlet.*;  
  3. importjavax.microedition.io.*;  
  4. importjava.io.*;  
  5.  
  6. publicclassSendTestMidletextendsMIDletimplementsCommandListener{  
  7. Displaydisplay=null;  
  8. TextFieldttest1,ttest2,ttest3,ttest4;  
  9. Formform;  
  10. Stringurl="http://202.103.191.61:80/test.jsp";  
  11. staticfinalCommandsend=newCommand("注册",Command.OK,2);  
  12. staticfinalCommandexit=newCommand("退出",Command.EXIT,2);  
  13. Stringmyname,pas1,pas2,test4;  
  14. publicSendTestMidlet(){  
  15. display=Display.getDisplay(this);  
  16. ttest1=newTextField("Name:","",25,TextField.ANY);  
  17. ttest2=newTextField("password:","",25,TextField.ANY);  
  18. ttest3=newTextField("password2:","",25,TextField.ANY);  
  19. ttest4=newTextField("note:","",25,TextField.ANY);  
  20. form=newForm("注册信息");  
  21. }  
  22. publicvoidstartApp()throwsMIDletStateChangeException{  
  23. form.append(ttest1);  
  24. form.append(ttest2);  
  25. form.append(ttest3);  
  26. form.append(ttest4);  
  27. form.addCommand(send);  
  28. form.addCommand(exit);  
  29. form.setCommandListener(this);  
  30. display.setCurrent(form);  
  31. }  
  32. publicvoidpauseApp(){  
  33. }  
  34. publicvoiddestroyApp(booleanunconditional){  
  35. notifyDestroyed();  
  36. }  
  37. publicvoidsendData(Stringurl)throwsIOException{  
  38. HttpConnectionconn=null;  
  39. InputStreaminput=null;  
  40. DataOutputStreamoutput=null;  
  41. StringBufferb=newStringBuffer();  
  42. TextBoxt=null;  
  43. try{  
  44. conn=(HttpConnection)Connector.open(url);  
  45. conn.setRequestMethod(HttpConnection.POST);  
  46. conn.setRequestProperty("IF-Modified-Since","29Dec200115:17:19GMT");  
  47. conn.setRequestProperty("User-Agent","Profile/MIDP-1.0Configuration/CLDC-1.0");  
  48. conn.setRequestProperty("Content-Language","en-CA");  
  49. conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
  50. output=conn.openDataOutputStream();  
  51. output.writeUTF("&name="+myname);  
  52. output.writeUTF("&pas1="+pas1);  
  53. output.writeUTF("&pas2="+pas2);  
  54. output.writeUTF("&test4="+test4);//.getBytes());  
  55. output.flush();  
  56. input=conn.openDataInputStream();  
  57. intch;  
  58. while((ch=input.read())!=-1){  
  59. b.append((char)ch);  
  60. System.out.print((char)ch);  
  61. }  
  62. t=newTextBox("Date",b.toString(),1024,0);  
  63. t.setCommandListener(this);  
  64. }  
  65. finally{  
  66. if(input!=null){  
  67. input.close();  
  68. }  
  69. if(output!=null){  
  70. output.close();  
  71. }  
  72. if(conn!=null){  
  73. conn.close();  
  74. }  
  75. }  
  76. display.setCurrent(t);  
  77. }  
  78.  
  79. publicvoidcommandAction(Commandconn,Displayabled){  
  80. Stringlabel=conn.getLabel();  
  81. if(label.equals("exit")){  
  82. destroyApp(true);  
  83. }elseif(label.equals("date?")){  
  84. myname=ttest1.getString();  
  85. pas1=ttest2.getString();  
  86. pas2=ttest3.getString();  
  87. test4=ttest4.getString();  
  88. try{  
  89. sendData(url);  
  90. }catch(IOExceptione){}  
  91. }  
  92. }  

正文到此结束
Loading...