黑莓中的Json Send / Post错误

时间:2012-04-30 08:09:54

标签: json blackberry

我想通过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获取此代码;

1 个答案:

答案 0 :(得分:0)

method的值为String,用于指定请求方法HttpConnection.GETHttpConnection.POST

您必须将其指定为:

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

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

您可以改为使用c.setRequestMethod()

查看更多here