【问题标题】:Send JSON Request from blackberry application从黑莓应用程序发送 JSON 请求
【发布时间】:2012-02-18 08:04:00
【问题描述】:

如何从作为客户端的黑莓应用程序向服务器发送 JSON 请求,以从服务器获取信息以在 BB 应用程序中使用它们 我在 windows 7 下使用 blackberry 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 后超时

任何帮助

【问题讨论】:

    标签: java json http blackberry blackberry-eclipse-plugin


    【解决方案1】:

    在模拟器中运行应用程序时,请确保您在 Eclipse 的“运行配置”或“调试配置”->“模拟器”中启用了使用模拟器启动移动数据系统连接服务 (MDS-CS)选项卡”->“常规选项卡”。

    如果未启用,您应该查看本指南“Testing a BlackBerry device application with the BlackBerry Smartphone Simulator”,特别是“测试使用 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. 点击应用。

    编辑
    或者,使用模拟器时,您可以将;deviceside=true 后缀添加到您传递给Connector.open() 的url。通过设置 deviceside=true,您可以指定应直接从手持设备(在您的情况下为模拟器)打开底层 TCP 连接,因此不会使用 BlackBerry MDS Connection Service。这是基于您的代码的代码 sn-p:

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

    希望这会有所帮助。

    【讨论】:

    • 我试试你的答案,但这并没有解决我的问题我启用了 MDS 但我仍然有错误
    • 启用 MDS 后是否重新启动了模拟器(关闭并重新运行和应用程序)?除非重新启动模拟器,否则它将无法工作。
    • 我重新启动 eclipse 和模拟器,但仍然出现错误 (BbiAuth:ERR:net.rim.device.api.crypto.bbiauth.BbiAuthException: No BBAuth service record configured) 此错误出现在程序崩溃后的模拟器
    • 能否请您在打开HttpConnection 时将urlPath 传递给Connector.open(urlPath) 方法。
    • @mbayloon 我猜为什么启用 MDS 对您不起作用。您是否偶然使用 JRE 7.0.0(9930 模拟器)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多