2017-04-19 49 views
0

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

“类型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); 

是否有任何替代办法做到这一点?请帮忙。在此先感谢

回答

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); 
+0

与Apache HttpClient也,我得到相同的错误,“该方法执行(HttpPost)是未定义的类型HttpClient” – user3640571

相关问题