无法执行请求 - 响应

时间:2011-08-23 13:32:55

标签: blackberry java-me blackberry-simulator

我为Blackberry中的客户端 - 服务器通信编写了以下代码,但它不起作用。

我通过POST发送数据。

在使用Blackberry模拟器进行测试时,我没有收到服务器的任何响应。

使用Android& iPhone,我能够从同一个服务器url&获得响应。具有相同的请求参数。

private void communicate() {

         HttpConnection hc = null; 
         DataInputStream dis = null;
         DataOutputStream dos = null;
         StringBuffer messagebuffer = new StringBuffer();
         try{
         String input="firstname="+ fName.getText().trim()+
         "&lastname=" + lName.getText().trim()+"&platform=blackberry";
         String url = "http://127.0.0.1:80/index/login";

         hc = (HttpConnection)
         Connector.open(url, Connector.READ_WRITE); // Set the request method
         hc.setRequestMethod(HttpConnection.POST);
         hc.setRequestProperty("User-Agent", "BlackBerry");
         hc.setRequestProperty("Content-Type",
         "application/x-www-form-urlencoded");
         hc.setRequestProperty("Content-Length",
         Integer.toString(input.length()));

         dos = hc.openDataOutputStream();
         dos.write(input.getBytes());
         dos.flush(); dos.close();
         // Retrieve the response back from the servlet
         dis = new DataInputStream(hc.openInputStream());
         int ch;
         // Check the Content-Length first
         long len = hc.getLength();
         if(len!=-1) {
         for(int i = 0;i<len;i++)

         if((ch = dis.read())!= -1)
         messagebuffer.append((char)ch);
         } else { // if the content-length is not available
         while ((ch = dis.read()) != -1)
         messagebuffer.append((char) ch);
         }
         dis.close();
         }catch(Exception e){
         e.printStackTrace();
         }
}

请建议我是否需要对代码进行任何更改。

提前致谢。

CB

1 个答案:

答案 0 :(得分:1)

MDS服务器不允许连接到127.0.0.1或localhost。

相关问题