Java实现百度统计api调用

1.需求:调用百度统计api获取统计数据

2.思路:看百度提供的api。结合网上大牛博客

3.实现如下

// 先写一个能调用https的工具类
public class HttpUtils {
	private static class TrustAnyTrustManager implements X509TrustManager {

        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // TODO Auto-generated method stub

        }

        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // TODO Auto-generated method stub

        }

        public X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }  

    }  

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {

        public boolean verify(String arg0, SSLSession arg1) {
            // TODO Auto-generated method stub
            return false;
        }  
    }  

    /** 
     * post方式请求服务器(https协议) 
     *  
     * @param url 
     *            请求地址 
     * @param content 
     *            参数 
     * @param charset 
     *            编码 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws KeyManagementException 
     * @throws IOException 
     */  
    public static byte[] post(String url, String content, String charset)  
            throws NoSuchAlgorithmException, KeyManagementException,  
            IOException {  
        SSLContext sc = SSLContext.getInstance("SSL");  
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },  
                new java.security.SecureRandom());  

        URL console = new URL(url);  
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();  
        conn.setSSLSocketFactory(sc.getSocketFactory());  
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());  
        conn.setDoOutput(true);  
        conn.connect();  
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());  
        out.write(content.getBytes(charset));  
        // 刷新、关闭  
        out.flush();  
        out.close();  
        InputStream is = conn.getInputStream();  
        if (is != null) {  
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            while ((len = is.read(buffer)) != -1) {  
                outStream.write(buffer, 0, len);  
            }  
            is.close();  
            return outStream.toByteArray();  
        }  
        return null;  
    }  
}
// 调用 https://api.baidu.com/json/tongji/v1/ReportService/getSiteList 接口获得siteId的列表 
public class Test {
	public static void main(String[] args) {
		try {
			JSONObject header = new JSONObject();
            // 账号名
			header.put("username", "baidu-铺海电子-A18KA1012");
			// 密码
            header.put("password", "Puhai2018");
			// 申请到的token值
            header.put("token", "73430b9205052155d105c7e32495e6be");
			header.put("account_type", "1");
			
			String urlStr = "https://api.baidu.com/json/tongji/v1/ReportService/getSiteList";
			String charset = "utf-8";
			
			JSONObject params = new JSONObject();
			params.put("header",header );
			
			byte[] res = HttpUtils.post(urlStr, params.toString(), charset);
			String s = new String(res);
			System.out.println(s);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
// 获取需要的统计数据
public class Test {
	public static void main(String[] args) {
		try {
			JSONObject header = new JSONObject();
			header.put("username", "baidu-铺海电子-A18KA1012");
			header.put("password", "Puhai2018");
			header.put("token", "73430b9205052155d105c7e32495e6be");
			header.put("account_type", "1");
			
			//String urlStr = "https://api.baidu.com/json/tongji/v1/ReportService/getSiteList";
			String urlStr = "https://api.baidu.com/json/tongji/v1/ReportService/getData";
			String charset = "utf-8";
			
			JSONObject body = new JSONObject();
			body.put("siteId", "12241771");
			body.put("method","overview/getTimeTrendRpt");//需要获取的数据
			body.put("start_date", "20180723");
			body.put("end_date", "20180724");
			body.put("metrics", "pv_count,visitor_count,ip_count"); //指标,数据单位
			
			
			
			JSONObject params = new JSONObject();
			params.put("header",header );
			params.put("body", body);
			
			
			byte[] res = HttpUtils.post(urlStr, params.toString(), charset);
			String s = new String(res);
			System.out.println(s);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

token值的获取方式:

1.百度统计页面 -- 管理模块 --- 数据导出服务 -- 开通token-- 获取token值

你可能感兴趣的:(百度统计)