问题
I want to send JSON request through HTTPConnection but i am getting error when i am trying to get the response code. Here is my code...
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);
}
}
i get this code from Send JSON request from blackberry used in postObject.put("method", method);
回答1:
The value of method
is a String
to specify the request method HttpConnection.GET
or HttpConnection.POST
.
You have to specify it like:
c.setRequestProperty("method", HttpConnection.GET);
or
c.setRequestProperty("method", HttpConnection.POST);
You can use c.setRequestMethod() instead.
See more here
来源:https://stackoverflow.com/questions/10380400/json-send-post-error-in-blackberry