Android 应用接入广点通统计API 方案

官方给你参考文档,很扯淡,是c++和python脚本:

安卓java代码接入如下:

package com.edaixi.util;

import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class TecentAdsUtil {

	private static Context mContext;

	public TecentAdsUtil(Context mContext) {
		super();
		TecentAdsUtil.mContext = mContext;
	}

	@SuppressWarnings("deprecation")
	public void getToTencentAds() throws SocketException {
		String encrypt_key = "*************";
		String IMEIString = getDeviceIMEI();
		String TimeString = getSystemTime();
		String query_string;
		try {
			query_string = "muid=" + URLEncoder.encode(IMEIString, "UTF-8")
					+ "&conv_time=" + URLEncoder.encode(TimeString, "UTF-8");

			String page = "http://t.gdt.qq.com/conv/app/appid" + "/conv?"
					+ query_string;
			String encode_page = URLEncoder.encode(page, "UTF-8");
			String property = "encrypt_key&GET&" + encode_page;
			String signature = string2MD5(property);

			String base_data = query_string + "&sign="
					+ URLEncoder.encode(signature, "UTF-8");
			String base64 = ToBase64(base_data, encrypt_key);
			String data = new String(Base64Util.encode(base64.getBytes()));
			String attachment = "conv_type="
					+ URLEncoder.encode("MOBILEAPP_ACTIVITE", "UTF-8")
					+ "&app_type=" + URLEncoder.encode("ANDROID", "UTF-8")
					+ "&advertiser_id=" + URLEncoder.encode("673411", "UTF-8");
			final String finalUrl = "http://t.gdt.qq.com/conv/app/1104311868/conv?v="
					+ data + "&" + attachment;

			HttpUtils http = new HttpUtils();
			http.configTimeout(10 * 1000);
			http.send(HttpMethod.GET, finalUrl, new RequestCallBack<String>() {

				@Override
				public void onStart() {
				}

				@Override
				public void onFailure(HttpException arg0, String result) {
				}

				@Override
				public void onSuccess(ResponseInfo<String> result) {
					LogUtil.e("===广点通返回的结果==="+result.result);
				}
			});
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	@SuppressLint("DefaultLocale")
	public static String getDeviceIMEI() {
		String imei = ((TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		return string2MD5(imei.toLowerCase());
	}

	// 获取当前时间戳
	public static String getSystemTime() {
		long time = System.currentTimeMillis() / 1000;
		return time + "";
	}

	// MD5工具类
	public static String string2MD5(String inStr) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();

	}

	// Base64工具类
	public static String ToBase64(String base_data, String encrypt_key) {
		char[] base_datachar = base_data.toCharArray();
		char[] base_datacharBak = new char[base_datachar.length];
		char[] encrypt_keychar = encrypt_key.toCharArray();
		int j = 0;
		for (int i = 0; i < base_datachar.length; i++) {
			base_datacharBak[i] = (char) (base_datachar[i] ^ encrypt_keychar[j]);
			j = (++j) % (encrypt_keychar.length);
		}
		return new String(base_datacharBak);
	}

}

  

 

package com.edaixi.util;

import java.io.UnsupportedEncodingException;

public class Base64Util {
	private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D',
			'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
			'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
			'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
			'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
			'4', '5', '6', '7', '8', '9', '+', '/' };

	private static byte[] base64DecodeChars = new byte[] { -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
			-1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59,
			60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
			10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1,
			-1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
			38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
			-1, -1 };

	public static String encode(byte[] data) {
		StringBuffer sb = new StringBuffer();
		int len = data.length;
		int i = 0;
		int b1, b2, b3;
		while (i < len) {
			b1 = data[i++] & 0xff;
			if (i == len) {
				sb.append(base64EncodeChars[b1 >>> 2]);
				sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
				sb.append("==");
				break;
			}
			b2 = data[i++] & 0xff;
			if (i == len) {
				sb.append(base64EncodeChars[b1 >>> 2]);
				sb.append(base64EncodeChars[((b1 & 0x03) << 4)
						| ((b2 & 0xf0) >>> 4)]);
				sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
				sb.append("=");
				break;
			}
			b3 = data[i++] & 0xff;
			sb.append(base64EncodeChars[b1 >>> 2]);
			sb.append(base64EncodeChars[((b1 & 0x03) << 4)
					| ((b2 & 0xf0) >>> 4)]);
			sb.append(base64EncodeChars[((b2 & 0x0f) << 2)
					| ((b3 & 0xc0) >>> 6)]);
			sb.append(base64EncodeChars[b3 & 0x3f]);
		}
		return sb.toString();
	}

	public static byte[] decode(String str) throws UnsupportedEncodingException {
		StringBuffer sb = new StringBuffer();
		byte[] data = str.getBytes("US-ASCII");
		int len = data.length;
		int i = 0;
		int b1, b2, b3, b4;
		while (i < len) {
			/* b1 */
			do {
				b1 = base64DecodeChars[data[i++]];
			} while (i < len && b1 == -1);
			if (b1 == -1)
				break;
			/* b2 */
			do {
				b2 = base64DecodeChars[data[i++]];
			} while (i < len && b2 == -1);
			if (b2 == -1)
				break;
			sb.append((char) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
			/* b3 */
			do {
				b3 = data[i++];
				if (b3 == 61)
					return sb.toString().getBytes("ISO-8859-1");
				b3 = base64DecodeChars[b3];
			} while (i < len && b3 == -1);
			if (b3 == -1)
				break;
			sb.append((char) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
			/* b4 */
			do {
				b4 = data[i++];
				if (b4 == 61)
					return sb.toString().getBytes("ISO-8859-1");
				b4 = base64DecodeChars[b4];
			} while (i < len && b4 == -1);
			if (b4 == -1)
				break;
			sb.append((char) (((b3 & 0x03) << 6) | b4));
		}
		return sb.toString().getBytes("ISO-8859-1");
	}
}

  

你可能感兴趣的:(android)