/*
微信公众平台开发的过程中 会很频繁的 做出对服务的get和post 请求 并对返回的结果进行解析
殷琮就把get和post 请求独立出来写成两个方法
每次调用的时候只需要给出接口的url
*/
/*
* get请求
*/
public static JSONObject doGetStr(String url){
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
JSONObject jsonObject=null;
try {
HttpResponse response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
if(entity!=null){
String result = EntityUtils.toString(entity,"utf-8");
jsonObject=JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
/*
* post请求
*/
public static JSONObject doPostStr(String url,String outString){
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
JSONObject jsonObject=null;
try {
httpPost.setEntity(new StringEntity(outString, "utf-8"));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity entity=response.getEntity();
String result=EntityUtils.toString(entity,"utf-8");
jsonObject=JSONObject.fromObject(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}