簡體   English   中英

從黑莓手機應用程序發送JSON請求

[英]Send JSON Request from blackberry application

我如何從充當客戶端的黑莓應用程序向服務器發送JSON請求以從服務器獲取信息以在BB應用程序中使用它們,我在Windows 7下使用黑莓Eclipse

我嘗試此代碼

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();
    }
   }

我在線程中通過run方法調用此方法

當模擬器到達( rc = c.getResponseCode(); )時,運行代碼停止

我調試代碼,並在到達此語句並出現此錯誤時停止

本地連接在〜120000之后超時

任何幫助

在模擬器中運行應用程序時,請確保在Eclipse的“運行配置”或“調試配置”->“模擬器選項卡”->“常規選項卡”中啟用了帶有模擬器啟動移動數據系統連接服務(MDS-CS)

如果未啟用,則應檢查本指南“ 使用BlackBerry Smartphone Simulator 測試BlackBerry設備應用程序 ”,尤其是“ 使用HTTP連接測試BlackBerry設備應用程序 ”部分。 簡而言之,您必須啟用MDS-CS。 啟用它之后,您應該重新啟動模擬器。 以下是本指南的引文:

啟動BlackBerry Smartphone Simulator時,啟動BlackBerry MDS Connection Service。

  1. 在Eclipse®中的“運行”菜單上,單擊“調試配置”或“運行配置”。
  2. 展開BlackBerry Simulator項目。
  3. 完成以下任務之一:
    • 要使用現有的啟動配置,請在BlackBerry Simulator下單擊啟動配置。
    • 要創建新的啟動配置,請右鍵單擊BlackBerry Simulator,然后選擇“新建”。
  4. 單擊模擬器選項卡。
  5. 單擊常規選項卡。
  6. 選中使用模擬器啟動移動數據系統連接服務(MDS-CS)復選框。
  7. 單擊應用。

編輯
或者,使用模擬器時,可以在傳遞給Connector.open()的URL中添加;deviceside=true后綴。 通過設置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);

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM