我正在使用 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();
关于黑莓中的json解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673279/