67,538
社区成员
发帖
与我相关
我的任务
分享
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置request和response编码格式为UTF-8
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
//获取素材数量
WeixinRequestService.getMaterialCount();
//获取永久图文素材
GetMaterial getMaterial = new GetMaterial("news", "0", "20");
WeixinRequestService.getPermanentMaterialList(getMaterial);
}
/**
* 获取永久素材列表
* @return JSONObject
* @author ZhangJintao
*/
public static JSONObject getPermanentMaterialList(GetMaterial getMaterial) {
String url = WeixinRequstURL.GETPERMANENTMATERIALLIST;
url = url.replaceAll("ACCESS_TOKEN", WeiXinConfig.AccessTOKEN);
JSONObject result = RequestUtils.doPostStr(url,JsonHelper.toJSON(getMaterial).toString());
System.out.println("获取永久素材列表返回结果 = " + result.toString());
return result;
}
/**
* 获取素材数量
* @return JSONObject
* @author ZhangJintao
*/
public static JSONObject getMaterialCount() {
System.out.println("WeixinRequestService.getMaterialCount()");
String url = WeixinRequstURL.GETMATERIALCOUNT;
url = url.replaceAll("ACCESS_TOKEN", WeiXinConfig.AccessTOKEN);
JSONObject result = RequestUtils.doPostStr(url,"");
System.out.println("获取素材数量返回结果 = " + result);
return result;
}
public static JSONObject doPostStr(String url , String outStr){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr, "utf-8"));
HttpResponse response = httpClient.execute(httpPost); //编号:1
String result = EntityUtils.toString(response.getEntity(),"utf-8"); //编号:2
// String result = "{\"success\":\"true\"}"; //编号:3
jsonObject = JSONObject.fromObject(result);
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}


