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){}
- }
- }
- }
【责任编辑:彭凡 TEL:(010)68476606】
文章标题:浅谈J2ME与JSP实现通信技巧
URL链接:http://www.shufengxianlan.com/qtweb/news48/423748.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联