本文共 4505 字,大约阅读时间需要 15 分钟。
百度统计接口调用三步:
调用登录接口获取ucid
调用站点查询列表获取站点列表(每个站点都具有一个siteId)
调用站点访问接口通过siteId查询访问数据
/** * 查询站点数据 * @param user 用户 * @param siteId 站点ID * @param startDate 开始时间 * @param endDate 结束时间 * @author shy * @date 2016-11-16 下午01:23:16 */ public static DataResult getSiteData(WebsiteUser user,Integer siteId,String startDate,String endDate,String gran){ Gson gson = new Gson(); AuthHeader header = new AuthHeader(); header.setUsername(user.getUserName()); header.setToken(user.getToken()); header.setPassword(user.getPassword()); header.setAccount_type(1); ApiRequest request = new ApiRequest(); GetDataParamBase body = new GetDataParamBase(); body.setSite_id(siteId); body.setMetrics(AppConstans.METRICS); body.setMethod(AppConstans.METHOD_TIME); body.setMax_result(0); // 默认查询全部 body.setGran(gran); //body.setOrder("visitor_count,desc"); //body.setStart_index(0); // 默认不分页 body.setStart_date(startDate); body.setEnd_date(endDate); request.setHeader(header); request.setBody(body); String json = gson.toJson(request); String resultStr = HttpClientUtils.doPost(user.getUcid(),AppConstans.GET_SITE_DATA, json.getBytes(), 20000, "utf-8"); System.out.println("data result ---siteId --:"+siteId+"startDate:"+startDate+"endDate:"+endDate+"--result:"+resultStr); ApiResponse resp = gson.fromJson(resultStr.replaceAll("\"--\"", "0"), new TypeToken(){}.getType()); GetDataResponse respResult = resp.getBody().getData().get(0); DataResult result = respResult.getResult(); if(null != resp && null != resp.getHeader() && resp.getHeader().getSucc()>0){ //百度返回数据中items 包含四个list 其中第一个是时间列表 第二个是数据列表 第三 第四均为空 List itemDate = (List) resp.getBody().getData().get(0).getResult().getItems().get(0); List itemData = (List) resp.getBody().getData().get(0).getResult().getItems().get(1); List listResult = new ArrayList(); DateCount dc; for(int i = 0 ;i < itemDate.size() ;i++){ dc = new DateCount(); dc.setDate(itemDate.get(i).get(0)); dc.setPv(getCountValue(itemData.get(i).get(0))); dc.setUv(getCountValue(itemData.get(i).get(1))); dc.setNv(getCountValue(itemData.get(i).get(2))); dc.setIp(getCountValue(itemData.get(i).get(3))); listResult.add(dc); } result.setDataList(listResult); } return result; }
其中常量参数
/**查询指标
* 必填
*取值范围:
*pageviews:浏览量(PV)
*visitors:访客数(UV)
*ips:IP数
*entrances:入口页次数
*outwards:贡献下游流量
*exits:退出页次数
*stayTime:平均停留时长
*exitRate:退出率
*/
public static final String METRICS = "pv_count,visitor_count,ip_count,new_visitor_count";
/**趋势数据*/
//public static final String METHOD_TIME = "overview/getTimeTrendRpt";//此方法只能查询单个指标
public static final String METHOD_TIME = "trend/time/a";
数据查询接口
public static final String GET_SITE_DATA = "
查询结果json串
data result ---siteId --:9776968startDate:20100101endDate:20161121--result:{"header":{"desc":"success","failures":[],"oprs":1,"succ":1,"oprtime":0,"quota":1,"rquota":49784,"status":0},"body":{"data":[{"result":{"total":83,"items":[[["2016/11/01 - 2016/11/21"],["2016/10/01 - 2016/10/31"],["2016/09/01 - 2016/09/30"],["2016/08/01 - 2016/08/31"],["2016/07/01 - 2016/07/31"],["2016/06/01 - 2016/06/30"],["2016/05/01 - 2016/05/31"],["2016/04/01 - 2016/04/30"],["2016/03/01 - 2016/03/31"],["2016/02/01 - 2016/02/29"],["2016/01/01 - 2016/01/31"],["2015/12/01 - 2015/12/31"],["2015/11/01 - 2015/11/30"],["2015/10/01 - 2015/10/31"],["2015/09/01 - 2015/09/30"],["2015/08/01 - 2015/08/31"],["2015/07/01 - 2015/07/31"],["2015/06/01 - 2015/06/30"],["2015/05/01 - 2015/05/31"],["2015/04/01 - 2015/04/30"]],[[613,139,62,138],[1127,141,82,140],[1673,160,100,151],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"],["--","--","--","--"]],[],[]],"timeSpan":["2010/01/01 - 2016/11/21"],"sum":[[3413,440,244,429],[]],"offset":0,"pageSum":[[3413,440,244,429],[],[]],"fields":["simple_date_title","pv_count","visitor_count","new_visitor_count","ip_count"]}}]}}
大家可以根据个人喜欢选择json解析工具进行数据解析,本文使用Gson....
备注WebUser
private String id;
private String userName;//百度统计账号
private String password;//百度统计密码
private String ucid;//百度统计用户ID
private String token;//百度统计token
转载地址:http://hitci.baihongyu.com/