0

我想通过 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); 中使用的黑莓发送 JSON 请求中获取此代码;

4

1 回答 1

0

的值methodString指定请求方法HttpConnection.GETHttpConnection.POST

您必须像这样指定它:

c.setRequestProperty("method", HttpConnection.GET);

或者

c.setRequestProperty("method", HttpConnection.POST);

您可以改用c.setRequestMethod()

在这里查看更多

于 2012-04-30T09:55:52.607 回答