将json字符串传递给Post方法

时间:2017-04-19 12:15:41

标签: java post http-post

我有一个json字符串,我想将它传递给POST方法。但是'执行'和' executeMethod'抛出错误如下:

" HttpClient类型中的方法execute(HttpUriRequest)不适用于参数(PostMethod)"。我已经包括了依赖性。

我的代码:

StringRequestEntity requestEntity = new StringRequestEntity(
            json-string,
            "application/json",
            "UTF-8");
PostMethod postMethod = new PostMethod("myUrl");
postMethod.setRequestEntity(requestEntity);
HttpResponse response = httpclient.execute(postMethod);

有没有其他方法可以做到这一点?请帮忙。提前谢谢

1 个答案:

答案 0 :(得分:0)

我使用Apache HttpClient。 用于调用post方法的代码段如下所示。

String JSON_STRING="{"name":"Example"}";
StringEntity requestEntity = new StringEntity(
JSON_STRING,ContentType.APPLICATION_JSON);
HttpPost postMethod = new HttpPost("http://example.com/action");
postMethod.setEntity(requestEntity);
HttpResponse rawResponse = httpclient.execute(postMethod);