我想通过HTTPConnection发送JSON请求,但是当我尝试获取响应代码时,我收到错误。 这是我的代码......
public void sendrequest(String url)throws IOException, JSONException
{
JSONObject postObject = new JSONObject();
postObject.put("method", method);
postObject.put("params", Parameters);
HttpConnection c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
c.setRequestProperty("Content-Length", "" + (postObject.toString().length() - 2));
c.setRequestProperty("method", method);
c.setRequestProperty("params", Parameters);
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK){
throw new IOException("HTTP response code: " + rc);
}
}
我从postObject.put(“method”,method)中使用Send JSON request from blackberry获取此代码;
答案 0 :(得分:0)
method
的值为String
,用于指定请求方法HttpConnection.GET
或HttpConnection.POST
。
您必须将其指定为:
c.setRequestProperty("method", HttpConnection.GET);
或
c.setRequestProperty("method", HttpConnection.POST);
您可以改为使用c.setRequestMethod()。
查看更多here