2012-02-18 209 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