2012-02-18 155 views
0

我怎样才能发送从作为客户端的工作到服务器,从服务器获取信息使用它们BB应用 我使用黑莓日食windows下的黑莓应用程序JSON请求7发送JSON请求

我尝试这个代码

public void loginRequest() throws IOException, JSONException{ 
    HttpConnection c = null; 
    InputStream is = null; 
    int rc; 

    JSONObject postObject = new JSONObject(); 
    postObject.put("method", method); 
    //postObject.put("params", Parameters); 

    try{ 
     c = (HttpConnection)Connector.open(urlPath); 

     // Set the request method and headers 
     c.setRequestMethod(HttpConnection.GET); 
     c.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); 
     c.setRequestProperty("Content-Length", "" + (postObject.toString().length() - 2)); 
     c.setRequestProperty("method", "GET"); 

     // Getting the response code will open the connection, 
     // send the request, and read the HTTP response headers. 
     // The headers are stored until requested. 
     rc = c.getResponseCode(); 
     if (rc != HttpConnection.HTTP_OK){ 
      throw new IOException("HTTP response code: " + rc); 
     } 

     is = c.openInputStream(); 

     // Get the length and process the data 
     int len = (int)c.getLength(); 
     if (len > 0){ 
      int actual = 0; 
      int bytesread = 0 ; 
      byte[] data = new byte[len]; 
      while ((bytesread != len) && (actual != -1)){ 
       actual = is.read(data, bytesread, len - bytesread); 
       bytesread += actual; 
      } 
      //Get the JSON String 
      System.out.println(new String(data)); 
     } 
     else{ 
      int ch; 
      while ((ch = is.read()) != -1){ 
       //TODO 
       /* 
       process((byte)ch); 
       */ 
      } 
     } 
    }catch (ClassCastException e){ 
     throw new IllegalArgumentException("Not an HTTP URL"); 
    }finally { 
     if (is != null) 
      is.close(); 
     if (c != null) 
      c.close(); 
    } 
    } 

i当模拟器距离(RC = c.getResponseCode();)调用由运行方法该方法在一个线程

运行的代码STO PS

我调试代码,当它与此错误

本地连接后〜120000

任何帮助

回答

6

当运行在模拟器中的应用程序超时达到这一说法停止,确保在Eclipse的“运行配置”或“调试配置” - >“模拟器选项卡” - >“常规选项卡”中启用启动移动数据系统连接服务(MDS-CS)和模拟器

如果未启用,请检查本指南“Testing a BlackBerry device application with the BlackBerry Smartphone Simulator”,尤其是“测试使用HTTP连接的BlackBerry设备应用程序”部分。长话短说,您必须启用MDS-CS。启用它后,你应该重新启动你的模拟器。下面是本指南报价:

启动BlackBerry MDS连接服务,当您启动BlackBerry智能手机模拟器

  1. 在Eclipse®,运行菜单上,单击调试配置或运行配置。
  2. 展开BlackBerry Simulator项目。
  3. 的完成以下任务之一:
    • 与现有的启动配置工作,在BlackBerry模拟器中,单击启动配置。
    • 要创建新的启动配置,请右键单击BlackBerry Simulator,选择新建。
  4. 单击模拟器选项卡。
  5. 单击常规选项卡。
  6. 选中带模拟器的启动移动数据系统连接服务(MDS-CS)复选框。
  7. 单击应用。

编辑
另外,使用模拟器时,你可以;deviceside=true后缀添加到您传递给Connector.open()的URL。通过设置deviceside = true您指定应直接从掌上电脑打开底层TCP连接(您的情况为模拟器),因此不会使用BlackBerry MDS Connection Service。这里是基于你的代码的代码片段:

if (DeviceInfo.isSimulator()) { 
    urlPath += ";deviceside=true"; 
} else { 
    urlPath += connectionDependentSuffix; // suffix that is relevant to 
              // the desired connection option 
} 
c = (HttpConnection)Connector.open(urlPath); 

希望这有助于。

+0

我试试你的答案,但你重新启动这并没有解决我的问题 启用MDS,但我仍然有错误 – 2012-02-18 10:00:46

+0

模拟器使MDS后(关闭并重新运行和应用程序)?除非模拟器重新启动,否则无法工作。 – mrvincenzo 2012-02-18 10:11:51

+0

我重新启动了eclipse和模拟器,并且仍然出现错误(BbiAuth:ERR:net.rim.device.api.crypto.bbiauth.BbiAuthException:没有配置BBAuth服务记录)此错误在程序崩溃后出现在模拟器的输出日志中 – 2012-02-18 10:32:14