json httpclient

来源:互联网 发布:nginx 更改根目录 编辑:程序博客网 时间:2024/06/10 09:12
public static JSONObject post(String url,JSONObject json){          HttpClient client = new DefaultHttpClient();          HttpPost post = new HttpPost(url);          JSONObject response = null;          try {              StringEntity s = new StringEntity(json.toString());              s.setContentEncoding("UTF-8");              s.setContentType("application/json");              post.setEntity(s);                            HttpResponse res = client.execute(post);              if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){                  HttpEntity entity = res.getEntity();                  String charset = EntityUtils.getContentCharSet(entity);                  response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset)));              }          } catch (Exception e) {              throw new RuntimeException(e);          }          return response;      }  

0 0