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;
}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值