问题描述
写了一个小Demo,实现点击确定联网后服务器返回相关信息的功能。但是很纠结,没错误却也不能联网。高手看下。究竟是哪的问题,请不吝赐教:publicclassgwymenuextendsFormimplementsCommandListener{//登陆privateTextFieldusertextbox;privateTextFieldpswdtextbox;HttpConnectionconn=null;InputStreamis=null;OutputStreamos=null;Stringurl="http://202.113.76.152/check.php";publicgwymenu(MIDletinstance,MyCanvascanvas){//加载背景try{topimage=Image.createImage("/top.png");btmimage=Image.createImage("/bottom.png");}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//加载顶色Mybgitemtopitem=newMybgitem(topimage,imag_W,imag_H);this.append(topitem);this.append(newSpacer(100,20));usertextbox=newTextField("用户名:","",15,TextField.ANY);this.append(usertextbox);this.append(newSpacer(100,10));pswdtextbox=newTextField("密码:","",15,TextField.PASSWORD);this.append(pswdtextbox);this.append(newSpacer(80,30));MyCustomItembtn=newMyCustomItem("登陆");this.append(btn);this.append(newSpacer(50,25));this.append(newChoiceGroup("",Choice.MULTIPLE,newString[]{"记住密码"},null));//加载底色Mybgitembtmitem=newMybgitem(btmimage,imag_W,imagbtm_H);this.append(btmitem);//增加按钮m_cmdBack=newCommand("返回",Command.BACK,1);m_cmdlogin=newCommand("确定",Command.OK,1);this.addCommand(m_cmdBack);this.addCommand(m_cmdlogin);this.setCommandListener(this);//设置焦点display.setCurrentItem(usertextbox);//显示Form屏幕display.setCurrent(this);}publicvoidcommandAction(Commandc,Displayabled){if(m_cmdBack==c){display.setCurrent(acanvas);}if(m_cmdlogin==c)/*点击确定接入网路,与服务器交互。*/{Stringname=usertextbox.getString().trim();Stringpassword=pswdtextbox.getString().trim();if(name.equals(""))return;byte[]temp=Loginfo2bytes(name,password);PostText(url,temp);GetText();//InfoFormiform=newInfoForm(display,this);//display.setCurrent(iform);}}/*转换对象数组为字节数组*/privatebyte[]Loginfo2bytes(Stringname,Stringpassword){//TODOAuto-generatedmethodstubbyte[]loginfo=null;//用于存放转换后的对象ByteArrayOutputStreambaos=newByteArrayOutputStream();//字节输出流DataOutputStreamdos=newDataOutputStream(baos);//数据输出流用于包装字节输出流try{dos.writeChars(name);dos.writeChars(password);loginfo=baos.toByteArray();//将写入的数据转换成字节数组dos.close();baos.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnloginfo;}publicvoidPostText(Stringurl2,finalbyte[]temp){//TODOAuto-generatedmethodstubnewThread(){publicvoidrun(){try{conn=(HttpConnection)Connector.open(url);conn.setRequestMethod(HttpConnection.POST);//发送信息os=conn.openOutputStream();for(inti=0;i<temp.length;i++){os.write(temp[i]);}os.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}.start();}publicvoidGetText(){//一般来说,操作网络等有阻塞的动作最好是启动一个线程。newThread(){publicvoidrun(){try{conn=(HttpConnection)Connector.open(url);conn.setRequestMethod(HttpConnection.POST);//设置请求方法,如果不设置的话,则系统默认的请求就是GET请求intsCode=conn.getResponseCode();if(sCode==200){//返回200时处理is=conn.openDataInputStream();byte[]sRecvData=readDataFromStream(is,512);if(sRecvData!=null){}elseSystem.out.println("Logfailed");}}catch(Exceptionex1){ex1.printStackTrace();}finally{//一般来说,close数据,一般写在finally方法,这样可以保证到如果程序出现任何问题都可以关闭连接,防止连接泄漏try{is.close();conn.close();}catch(IOExceptionex){}}}}.start();}finalbyte[]readDataFromStream(InputStreamaIs,intaCache){//TODOAuto-generatedmethodstubbyte[]sData=null;ByteArrayOutputStreambos=newByteArrayOutputStream();try{byte[]sCaceh=newbyte[aCache];intpos=0;inthasRead=0;while((pos=aIs.read(sCaceh,0,aCache))!=-1){hasRead+=pos;bos.write(sCaceh,0,pos);}sData=bos.toByteArray();}catch(Exceptione){}returnsData;}}
解决方案
解决方案二:
Stringurl="http://202.113.76.152/check.php";为何自己不通过tomcat搞个localhost?或者你换一个靠谱点的http://baidu.com试试
解决方案三:
可以了,连服务器成功。