1. 程式人生 > >httpclient傳json字串raw形式,調post

httpclient傳json字串raw形式,調post

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();
}    
}