HttpPost发送Json

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。