关于使用java获取百度统计Api数据

Java调用百度统计API获取数据分析
本文介绍如何使用Java编程语言与百度统计API进行集成,详细阐述了调用过程和关键步骤,包括授权认证、请求参数设置以及数据解析,旨在帮助开发者高效地获取和分析网站统计数据。
import com.baidu.api.client.core.JacksonUtil;
import net.sf.json.JSONObject;
import java.util.LinkedHashMap;
import java.util.Map;

public static void main(String args[]){
        try{
            Map<String, String> header = new LinkedHashMap<String, String>();
            header.put("username","username");
            header.put("password","password");
            header.put("token","token");
            header.put("account_type","1");

            Map<String, String> body = new LinkedHashMap<String, String>();
            body.put("siteId","siteId");
            body.put("method","overview/getTimeTrendRpt");
            body.put("start_date","20170627");
            body.put("end_date","20170703");
            body.put("metrics","pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time");

            Map<String, Object> params = new LinkedHashMap<String, Object>();
            params.put("header", header);
            params.put("body", body);


            //站点列表:https://api.baidu.com/json/tongji/v1/ReportService/getSiteList
            //站点数据:https://api.baidu.com/json/tongji/v1/ReportService/getData

            //获取的数据
            String results = HttpUtil.doPostSSL("https://api.baidu.com/json/tongji/v1/ReportService/getData", json);




        }catch (Exception e){
            e.printStackTrace();
        }
    }





   /**
     * 发送 SSL POST 请求(HTTPS),JSON形式
     * @param apiUrl API接口URL
     * @param json JSON对象
     * @return
     */
    public static String doPostSSL(String apiUrl, Object json) {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(HttpUtil.createSSLConnSocketFactory()).setConnectionManager(HttpUtil.connMgr).setDefaultRequestConfig(HttpUtil.requestConfig).build();

        HttpPost httpPost = new HttpPost(apiUrl);
        CloseableHttpResponse response = null;
        String httpStr = null;

        try {
            httpPost.setConfig(HttpUtil.requestConfig);
            StringEntity stringEntity = new StringEntity(json.toString(),"UTF-8");//解决中文乱码问题
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != org.apache.http.HttpStatus.SC_OK) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity == null) {
                return null;
            }
            httpStr = EntityUtils.toString(entity, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpStr;
    }




结果
{
     "header":
    {
        "desc":"success",
         "failures":[],
         "oprs":1,
         "succ":1,
         "oprtime":0,
         "quota":1,
         "rquota":49990,
         "status":0
     },
     "body":
     {
         "data":
            [
             {"result":
                {"items":
                 [
                    [
                        ["2017/06/27"],
                        ["2017/06/28"],
                        ["2017/06/29"],
                        ["2017/06/30"],
                        ["2017/07/01"],
                        ["2017/07/02"],
                        ["2017/07/03"]
                    ],
                    [
                        [10,20,30,40.1,50],
                        [10,20,30,40.2,50],
                        [10,20,30,40.3,50],
                        [10,20,30,40.4,50],
                        [10,20,30,40.5,50],
                        [10,20,30,40.6,50],
                        [10,20,30,40.7,50]
                    ],
                    [],
                    []
                ],
                "timeSpan":["2017/06/27 - 2017/07/03"],
                "fields":["simple_date_title","pv_count","visitor_count","ip_count","bounce_ratio","avg_visit_time"]
                }
             }
            ]
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值