【问题标题】:json parsing in blackberry?在黑莓中解析json?
【发布时间】:2012-05-21 02:09:31
【问题描述】:

我正在开发一个使用 JSON 的 Web 服务应用程序。 在执行任务时,我成功地通过点击 URL 直接获取 JSOn 响应。

现在我有一个使用请求参数请求的任务。

enter code here
private void callJSON_Webservice(String method,String paraLastModifiedDate) {
        HttpConnection c=null;
        InputStream is = null;
        String feedURL = Constants.feedURL;
        int rc;

        try{
            JSONObject postObject = new JSONObject();
            postObject.put("CheckLatestDataDate",method);
            postObject.put("LastModifiedDate", paraLastModifiedDate);
            //c = new HttpConnectionFactory().getHttpConnection(feedURL);
            c = (HttpConnection)Connector.open(feedURL + ConnectionManager.getConnectionString());

            // 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", HttpConnection.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();

            String json = StringUtils.convertStreamToString(is);
            object = new JSONObject(json);


        }catch (Exception e) {
            System.out.println(e+"call webservice exception");
        }

    }

使用此代码,我得到 EOF 异常。我需要尽快完成这个小任务。请帮我...! 提前感谢

【问题讨论】:

    标签: json blackberry


    【解决方案1】:

    尝试替换

    is = c.openInputStream();
    
    String json = StringUtils.convertStreamToString(is);
    

    以下内容:

    is = c.openInputStream();
    
    StringBuffer buffer = new StringBuffer();
    int ch = 0;
    while (ch != -1) {
        ch = is.read();
        buffer.append((char) ch);
    }
    
    String json = buffer.toString();
    


    参考:convert StreamConnection to String

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多