jeecms框架系統調用百度統計接口
1.創建一個能調用https的工具類
package com.jeecms.common.util;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
// 先寫一個能調用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 String 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);
}
byte data[] = outStream.toByteArray();
String str = new String(data,charset); //解決輸出中文亂碼
outStream.close();
is.close();
return str.toString();
}
return null;
}
}
- 調用 https://api.baidu.com/json/tongji/v1/ReportService/getData接口獲得siteId的列表
body中method參數和metrics參數,這兩個參數官方文檔中有這裏是鏈接:
http://tongji.baidu.com/open/api/more?p=tongjiapi_getData.tpl
package com.jeecms.cms.api.admin.main;
import com.jeecms.common.util.HttpUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
@Controller
public class BaiduApiContent {
public static final String username = "";//用戶名
public static final String password = "";//密碼
public static final String token = "";//token值的獲取方式:百度統計頁面 -- 管理模塊 --- 數據導出服務 -- 開通token-- 獲取token值
public static final String siteId = "";//網站地址上面就有siteid
public static final String urlStr = "https://api.baidu.com/json/tongji/v1/ReportService/getData";
public static final String charset = "utf-8";
@RequestMapping(value = "/getTimeTrendRpt/list")//趨勢分析
@ResponseBody
public String getTimeTrendRpt() throws Exception{
String method="trend/time/a";
String metrics="pv_count,visitor_count,ip_count";
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Date time=cal.getTime();
String startDate=new SimpleDateFormat("yyyyMMdd").format(time);
String endDate=new SimpleDateFormat("yyyyMMdd").format(time);
JSONObject header = new JSONObject();
header.put("username", username);//用戶名
header.put("password", password);//用戶密碼
header.put("token", token);//申請到的token
header.put("account_type", "1");
JSONObject body = new JSONObject();
body.put("siteId", siteId);
body.put("method", method);//需要獲取的數據
body.put("start_date", startDate);
body.put("end_date", endDate);
body.put("metrics", metrics); //指標,數據單位
JSONObject params = new JSONObject();
params.put("header", header);
params.put("body", body);
// String s = HttpUtils.post(urlStr, params.toString(), charset);
// String s = new String(res);
/* System.out.println(s);*/
return HttpUtils.post(urlStr, params.toString(), charset);
}
@RequestMapping(value = "/getCommonTrackRpt/list")//來源網站、搜索詞、入口頁面、受訪頁面
@ResponseBody
public String getCommonTrackRpt() throws Exception{
String method="overview/getCommonTrackRpt";
String metrics="pv_count";
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Date time=cal.getTime();
String startDate=new SimpleDateFormat("yyyyMMdd").format(time);
String endDate=new SimpleDateFormat("yyyyMMdd").format(new Date());
JSONObject header = new JSONObject();
header.put("username", username);//用戶名
header.put("password", password);//用戶密碼
header.put("token", token);//申請到的token
header.put("account_type", "1");
JSONObject body = new JSONObject();
body.put("siteId", siteId);
body.put("method", method);//需要獲取的數據
body.put("start_date", startDate);
body.put("end_date", endDate);
body.put("metrics", metrics); //指標,數據單位
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);*/
return HttpUtils.post(urlStr, params.toString(), charset);
}
@RequestMapping(value = "/getDistrictRpt/list")//地域分佈)
@ResponseBody
public String getDistrictRpt() throws Exception{
String method="overview/getDistrictRpt";
String metrics="pv_count";
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Date time=cal.getTime();
String startDate=new SimpleDateFormat("yyyyMMdd").format(time);
String endDate=new SimpleDateFormat("yyyyMMdd").format(time);
JSONObject header = new JSONObject();
header.put("username", username);//用戶名
header.put("password", password);//用戶密碼
header.put("token", token);//申請到的token
header.put("account_type", "1");
JSONObject body = new JSONObject();
body.put("siteId", siteId);
body.put("method", method);//需要獲取的數據
body.put("start_date", startDate);
body.put("end_date", endDate);
body.put("metrics", metrics); //指標,數據單位
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);*/
return HttpUtils.post(urlStr, params.toString(), charset);
}
}
3.配置xml文件
<bean id="baiduApiContent" class="com.jeecms.cms.api.admin.main.BaiduApiContent"/>
4.vue獲取接口返回的json數據進行數據截取 並在頁面展示出來