public static String result;
public static void httpTest() throws ClientProtocolException, IOException {
String json = "{\"appId\":\"ddd\",\"usrId\":\"ddd\",\"latitude\":\"123\",\"longitude\":\"123\",\"range\":\"123\"}";
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://.............");
StringEntity postingString = new StringEntity(json);
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity());
//
System.out.println(content);
result = content;
}
public static void main(String[] args) {
try {
httpTest();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void httpTest() throws ClientProtocolException, IOException {
String json = "{\"appId\":\"ddd\",\"usrId\":\"ddd\",\"latitude\":\"123\",\"longitude\":\"123\",\"range\":\"123\"}";
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://.............");
StringEntity postingString = new StringEntity(json);
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity());
//
System.out.println(content);
result = content;
}
public static void main(String[] args) {
try {
httpTest();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这篇博客展示了如何使用HttpClient库以RAW形式发送JSON字符串进行POST请求。代码中定义了一个JSON对象,创建了HttpPost实例,并设置了Content-type为application/json。执行请求后,将响应内容打印并存储到变量中。
2752

被折叠的 条评论
为什么被折叠?



