zoukankan      html  css  js  c++  java
  • Http发送Json

     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

  • 相关阅读:
    数组列简介
    linq的使用
    StringBuilder对象
    使用类来继承接口
    设置函数库并引用
    循环语句
    cut和paste用法
    uniq用法
    shell中数组的应用
    委派
  • 原文地址:https://www.cnblogs.com/hym-pcitc/p/5674324.html
Copyright © 2011-2022 走看看