1. 程式人生 > >java對接騰訊廣點通API介面(上報行為資料)

java對接騰訊廣點通API介面(上報行為資料)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.Logger;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class GDTUtil {
private static String URL = "https://api.e.qq.com/v1.0/user_actions/add";


public static String guangDianTong(HttpServletRequest request) throws Exception {
String url = request.getHeader("referer");
String click_id = "qianduoduo";
try {
click_id = TruncateUrlPage(url);//獲取url?後的click_id 
} catch (Exception e) {
click_id = "qianduoduo";
}
// 拼接url
Map<String, Object> m = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("click_id", click_id);
JSONObject jb = JSONObject.fromObject(maps);
map.put("user_action_set_id", "8888888");
map.put("url", url);// 要轉化的頁面連結地址
map.put("action_time", System.currentTimeMillis() / 1000);
map.put("action_type", "COMPLETE_ORDER");// 上報行為資料型別
map.put("trace", jb);
m.put("account_id", "777777");//廣告主ID
JSONArray json = JSONArray.fromObject(map);
m.put("actions", json);
JSONObject jnb = JSONObject.fromObject(m);
return sendPost(URL, jnb.toString());


}


public static String sendPost(String url, String param) {
String accessToken = AccessTokenUtil.getGDTAccessToken();
StringBuffer sb = new StringBuffer();
sb.append(url);
sb.append("?access_token=");
sb.append(accessToken);
sb.append("&timestamp=");
sb.append(System.currentTimeMillis() / 1000);
sb.append("&nonce=");
sb.append(System.currentTimeMillis() / 1000);
Logger log = Logger.getLogger(GDTUtil.class);
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(sb.toString());
// 開啟和URL之間的連線
URLConnection conn = realUrl.openConnection();
// 傳送POST請求必須設定如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json");
// 獲取URLConnection物件對應的輸出流(設定請求編碼為UTF-8)
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
// 傳送請求引數
out.print(param);
// flush輸出流的緩衝
out.flush();
// 獲取請求返回資料(設定返回資料編碼為UTF-8)
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
log.info("返回結果*****************************>" + result);


} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}


return result;
}


// 解析url
public static String TruncateUrlPage(String strURL) {
String strAllParam = null;
String[] arrSplit = null;
strURL = strURL.trim().toLowerCase();
arrSplit = strURL.split("[?]");
if (strURL.length() > 1) {
if (arrSplit.length > 1) {
for (int i = 1; i < arrSplit.length; i++) {
strAllParam = arrSplit[i];
}
}
}
String[] split = strAllParam.split("=");
if (split.length > 1) {
for (int i = 1; i < split.length; i++) {
strAllParam = split[i];
}
}
return strAllParam;
}
}