博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
百度统计接口调用——获取站点访问数据
阅读量:4052 次
发布时间:2019-05-25

本文共 4505 字,大约阅读时间需要 15 分钟。

百度统计接口调用三步:

  1. 调用登录接口获取ucid

  2. 调用站点查询列表获取站点列表(每个站点都具有一个siteId)

  3. 调用站点访问接口通过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/

你可能感兴趣的文章
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>