2

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

4

1 回答 1

2

尝试更换

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


参考:将 StreamConnection 转换为 String

于 2012-05-20T18:09:31.180 回答