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值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章