黑莓中的json解析?

标签 json blackberry

我正在使用 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 异常。我需要尽快完成这个小任务。请帮我...! 提前致谢

最佳答案

尝试更换

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

关于黑莓中的json解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673279/

相关文章:

javascript - 为什么 v8::JSON::Parse 从 v8 比 NodeJS JSON::parse 慢得多?

blackberry - BlackBerry 中的图像按钮

android - 除了使用 cookie 之外,还有什么方法可以识别手机网络用户?

c# - 如何将 JSON 值保留为字符串?

json - 获取本地目录路径和里面的所有文件名(Swift)

php - jQuery 解析 Twitter jSON 但不解析我的同一个 PHP 文档

json - 解码异构数组

java - 黑莓中的图像浏览器

java - AddFolderListener 在某些设备中有效,但在其他设备中无效

json - 使用 org.json.me 解析 JSON 是否有字符串长度限制