我有类似以下内容:
final String url = "http://example.com";
final HttpClient httpClient = new HttpClient();
final PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader("Content-Type", "application/json");
postMethod.addParameters(new NameValuePair[]{
new NameValuePair("name", "value)
});
httpClient.executeMethod(httpMethod);
postMethod.getResponseBodyAsStream();
postMethod.releaseConnection();
它不断返回500。服务提供商说我需要发送JSON。Apache HttpClient 3.1+如何完成?
NameValuePair
只需添加一个request参数,就不会在代码中发送任何JSON。服务期望接收什么JSON结构,您要发送什么数据?您正在寻找postMethod.setRequestEntity()
一个StringRequestEntity
包含您的JSON。