日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺談J2ME與JSP實(shí)現(xiàn)通信技巧

J2ME與JSP實(shí)現(xiàn)通信首先當(dāng)然是要用以下三個(gè)類了:

 
 
 
  1. HttpConnectionconn=null;//用于連接到web服務(wù)  
  2. InputStreaminput=null;//用于接收返回信息  
  3. DataOutputStreamoutput=null;//用于發(fā)送數(shù)據(jù) 

(當(dāng)然也可以用OutputStream,只是DataOutputStream有更多實(shí)用的方法)然后就是用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地址作為參數(shù)一定要加上端口號(hào),用網(wǎng)址可不用默認(rèn)就是80嘛!

接著設(shè)置web服務(wù)接收的一些參數(shù)

 
 
 
  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"); 

打開輸出流,傳數(shù)據(jù)

 
 
 
  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(); 

到這里實(shí)際上就是我們?cè)跒g覽器中輸入http//202.103.191.61:80/test.jsp&test1=test1&test2=test2&test3=test3&test4=test4
注意到?jīng)]有test.jsp后面全是&參數(shù)名=值第一個(gè)不是以?開頭(但如果參數(shù)是只有一個(gè)或兩個(gè)時(shí)可以不第一個(gè)不用&都行,不知道為什么)!

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

 
 
 
  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();來取數(shù)據(jù),然后進(jìn)行處理啦,就不多說了!

附J2ME與JSP實(shí)現(xiàn)通信源碼

 
 
 
  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("注冊(cè)",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("注冊(cè)信息");  
  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. }  

【責(zé)任編輯:彭凡 TEL:(010)68476606】


分享名稱:淺談J2ME與JSP實(shí)現(xiàn)通信技巧
鏈接分享:http://www.5511xx.com/article/coijiic.html