首页 > Web开发 > 详细

httpClient发送Json请求,结果返回Json.

时间:2014-11-13 12:11:36      阅读:333      评论:0      收藏:0      [点我收藏+]
  1. public static JSONObject post(String url,JSONObject json){  
  2.         HttpClient client = new DefaultHttpClient();  
  3.         HttpPost post = new HttpPost(url);  
  4.         JSONObject response = null;  
  5.         try {  
  6.             StringEntity s = new StringEntity(json.toString());  
  7.             s.setContentEncoding("UTF-8");  
  8.             s.setContentType("application/json");  
  9.             post.setEntity(s);  
  10.               
  11.             HttpResponse res = client.execute(post);  
  12.             if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){  
  13.                 HttpEntity entity = res.getEntity();  
  14.                 String charset = EntityUtils.getContentCharSet(entity);  
  15.                 response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset)));  
  16.             }  
  17.         } catch (Exception e) {  
  18.             throw new RuntimeException(e);  
  19.         }  
  20.         return response;  
  21.     }  

httpClient发送Json请求,结果返回Json.

原文:http://www.cnblogs.com/sophelia-M/p/4094318.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!