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
    评论
广点通 API 对接文档是腾讯广点通广告平台提供给开发人员的一个重要参考文件,用于帮助开发人员对接广点通广告平台的相关接口和功能。文档中提供了详细的接口说明、参数说明、返回值说明以及示例代码等内容,方便开发人员了解和使用广点通的功能和接口广点通 API 对接文档主要包括以下几个方面的内容: 1. 接口概述:介绍了广点通广告平台的基本信息、使用限制、申请和获取开发者账号的流程等内容。 2. 接口说明:列出了广点通广告平台支持的各类接口,并对每个接口进行了详细的解释,包括接口名称、调用方式、参数说明、返回值说明等。 3. 参数说明:对接口中涉及到的参数进行了详细的解释和说明,包括参数名称、数据类型、是否必填、取值范围等。 4. 返回值说明:对接口调用返回的结果进行了详细的解释和说明,包括返回值的数据结构、数据类型、各字段的含义等。 5. 示例代码:提供了一些常见的接口调用示例代码,方便开发人员直接参考和使用。 6. 常见问题:列出了一些开发人员在对接过程中常见的问题和解决方案,帮助开发人员快速解决问题。 通过阅读广点通 API 对接文档,开发人员可以详细了解广点通广告平台的功能和接口,了解如何调用和使用接口,快速上手开发。同时,文档还提供了示例代码和常见问题解答,方便开发人员参考和解决问题。因此,广点通 API 对接文档是开发人员在对接广点通广告平台时不可或缺的重要参考资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值