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(),"UTF-8");  //服务器返回中文乱码,加上"UTF-8"可以了
 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         }  

http://201202104504.iteye.com/blog/1413714

 

posted on 2016-07-15 17:30  筱筱鹿丸  阅读(714)  评论(0编辑  收藏  举报