微信开发3-------------------------------获得每天用户关注/取关/总人数的统计

来源:互联网 发布:什么是淘宝网店 编辑:程序博客网 时间:2024/04/30 15:26

package cn.xxxx.utils;import javax.annotation.Resource;import org.codehaus.jettison.json.JSONArray;import org.codehaus.jettison.json.JSONException;import org.codehaus.jettison.json.JSONObject;import org.junit.Test;import cn.xxxxx.domain.Userdata;import cn.xxxxx.UserdataService;public class EveryData {// 访问每天变化的urlprivate static final String USERSUBDATAURL = "https://api.weixin.qq.com/datacube/getusersummary";// 访问每天总数量的urlprivate static final String USERALLDATAURL = "https://api.weixin.qq.com/datacube/getusercumulate";//昨天的日期private String yestoday = NowTimeUtils.getYestoday();//service层接口@Resourceprivate UserdataService uds;//注入的动态获取token的bean@Resourceprivate WeixinBean bean;/** * 每天凌晨读取昨天用户统计数据并存储到数据库中,该方法通过定时器每天凌晨0点5分执行 *  * @throws Exception */@Testpublic void getUserDataWithEveryDay() throws Exception {JSONObject jsonobject = new JSONObject();jsonobject.put("begin_date", yestoday);jsonobject.put("end_date", yestoday);//获得每天新增关注的人数Integer newuser = Integer.parseInt(getKv(jsonobject, "new_user",USERSUBDATAURL));//获得昨天取消关注的人数Integer canceluser = Integer.parseInt(getKv(jsonobject, "cancel_user",USERSUBDATAURL));//获得总的人数Integer cumulateuser = Integer.parseInt(getKv(jsonobject,"cumulate_user", USERALLDATAURL));               //表关联对象Userdata ud = new Userdata();ud.setDay(yestoday);ud.setCanceluser(canceluser);ud.setCumulateuser(cumulateuser);ud.setNewuser(newuser);//插入数据库uds.insert(ud);}private String getKv(JSONObject jsonobject, String key, String url) {//发送请求,携带json参数url=url+"?access_token="+bean.getToken();String result = HttpUtils.sendJson(url, jsonobject);JSONObject jsonObject;String value = null;try {jsonObject = new JSONObject(result);JSONArray jsonArray = (JSONArray) jsonObject.get("list");//如果里面没有,那么新增和取关都设置为0if (jsonArray == null || jsonArray.length() <= 0) {return "0";}jsonObject = (JSONObject) jsonArray.get(0);value = jsonObject.getString(key);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return value;}}

二话不说,直接粘代码体现思路,但其中一些类是自有的,涉及公司命名等,不能粘贴


阅读全文
1 0
原创粉丝点击