调用接口

1、ReportData=======

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONObject;

public class ReportData {

	public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
		String id;
		for (String account : BaiduAccount.ACCOUNT_LIST) {
			id = getID(account);
			getState(id, account);
			getResult(id, account);
		}
		System.out.println(BaiduAccount.ACCOUNT_LIST.length + "个账号文件全部写入完毕");
	}

	public static String getID(String accountStr) throws ClientProtocolException, IOException {

		String url = "接口";

		StringBuffer request = new StringBuffer();
		request.append(swap("reportRequestType")).append(":{");
		request.append(swap("performanceData")).append(":[");
		request.append(swap("impression")).append(",");
		request.append(swap("click")).append(",");
		request.append(swap("cpm")).append(",");
		request.append(swap("ctr")).append(",");
		request.append(swap("conversion")).append(",");
		request.append(swap("cpc")).append(",");
		request.append(swap("cost")).append("],");
		request.append(swap("levelOfDetails")).append(":").append("11").append(",");
		request.append(swap("startDate")).append(":").append(swap(BaiduAccount.SEARCH_BEGINTIME)).append(",");
		request.append(swap("endDate")).append(":").append(swap(BaiduAccount.SEARCH_ENDTIME)).append(",");
		request.append(swap("unitOfTime")).append(":").append("5").append(",");
		request.append(swap("reportType")).append(":").append("14").append(",");
		request.append(swap("device")).append(":").append("0");
		request.append("}").append("}");

		StringBuffer sb = new StringBuffer();
		sb.append("{").append(swap("header")).append(":{");
		sb.append(swap("username")).append(":").append(swap(BaiduAccount.BAIDU_MANAGER)).append(",");
		sb.append(swap("password")).append(":").append(swap(BaiduAccount.BAIDU_MANAGER_PASSWD)).append(",");
		sb.append(swap("token")).append(":").append(swap(BaiduAccount.BAIDU_TOKEN)).append(",");
		sb.append(swap("target")).append(":").append(swap(accountStr));
		sb.append("},").append(swap("body")).append(":{");
		sb.append(request.toString()).append("}");

		String response = HttpUtil.post(url, sb.toString());

		// 解析json
		JSONObject root = new JSONObject(response);
		JSONObject header = root.getJSONObject("header");
		String desc = header.getString("desc");

		String reportId = null;
		if (desc.equals("success")) {
			JSONObject body = root.getJSONObject("body");
			JSONArray data = body.getJSONArray("data");
			JSONObject Id = data.getJSONObject(0);
			reportId = Id.getString("reportId");
		} else {
			System.out.println("error: getID:" + accountStr + "\n" + response);
		}
		return reportId;
	}

	public static void getState(String id, String account)
			throws ClientProtocolException, IOException, InterruptedException {

		String result;

		StringBuffer request = new StringBuffer();
		request.append(swap("reportId")).append(":").append(swap(id));

		StringBuffer sb = new StringBuffer();
		sb.append("{").append(swap("header")).append(":{");
		sb.append(swap("username")).append(":").append(swap(BaiduAccount.BAIDU_MANAGER)).append(",");
		sb.append(swap("password")).append(":").append(swap(BaiduAccount.BAIDU_MANAGER_PASSWD)).append(",");
		sb.append(swap("token")).append(":").append(swap(BaiduAccount.BAIDU_TOKEN)).append(",");
		sb.append(swap("target")).append(":").append(swap(account));
		sb.append("},").append(swap("body")).append(":{");
		sb.append(request.toString()).append("}}");

		while (true) {
			result = HttpUtil.post("接口", sb.toString());

			JSONObject root = new JSONObject(result);
			JSONObject header = root.getJSONObject("header");
			String desc = header.getString("desc");

			if (desc.equals("success")) {
				JSONObject body = root.getJSONObject("body");
				JSONArray data = body.getJSONArray("data");
				JSONObject status = data.getJSONObject(0);
				int statusId = status.getInt("isGenerated");
				if (statusId == 3) {
					break;
				}
			} else {
				System.out.println("error: id: " + id + ", account: " + account + "\n" + result);
			}
			Thread.sleep(5000);
		}
	}

	private static void getResult(String id, String account) throws ClientProtocolException, IOException {

		String url = "接口";

		StringBuffer request = new StringBuffer();
		request.append(swap("reportId")).append(":").append(swap(id));

		StringBuffer sb = new StringBuffer();
		sb.append("{").append(swap("header")).append(":{");
		sb.append(swap("username")).append(":").append(swap(BaiduAccount.BAIDU_MANAGER)).append(",");
		sb.append(swap("password")).append(":").append(swap(BaiduAccount.BAIDU_MANAGER_PASSWD)).append(",");
		sb.append(swap("token")).append(":").append(swap(BaiduAccount.BAIDU_TOKEN)).append(",");
		sb.append(swap("target")).append(":").append(swap(account));
		sb.append("},").append(swap("body")).append(":{");
		sb.append(request.toString()).append("}}");

		String response = HttpUtil.post(url, sb.toString());

		// 解析json
		JSONObject root = new JSONObject(response);
		JSONObject header = root.getJSONObject("header");
		String desc = header.getString("desc");

		if (desc.equals("success")) {
			JSONObject body = root.getJSONObject("body");
			JSONArray data = body.getJSONArray("data");
			JSONObject Id = data.getJSONObject(0);
			String path = Id.getString("reportFilePath");
			InputStream in = HttpUtil.getInputStream(path);
			HttpUtil.inputStream2File(in, BaiduAccount.BAIDU_SAVE + File.separator + account + id + ".csv");
			System.out.println(BaiduAccount.BAIDU_SAVE + File.separator + account + id + ".csv 文件写入完毕");
		} else {
			System.out.println("error: getResult: " + account + "\n" + response);
		}
	}
}

2、BaiduAccount=========

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class BaiduAccount {
	public static String BAIDU_MANAGER;
	public static String BAIDU_MANAGER_PASSWD;
	public static String[] ACCOUNT_LIST;
	public static String BAIDU_TOKEN;
	public static String SEARCH_BEGINTIME;
	public static String SEARCH_ENDTIME;
	public static String BAIDU_SAVE;

	static {
		Properties property = new Properties();
		try {
			property.load((new InputStreamReader(new FileInputStream(new File("./baidu.properties")), "utf-8")));
			BAIDU_MANAGER = property.getProperty("baidu_manager");
			BAIDU_MANAGER_PASSWD = property.getProperty("baidu_manager_passwd");
			String array = property.getProperty("baidu_sub_account");
			ACCOUNT_LIST = array.split(",");
			BAIDU_TOKEN = property.getProperty("baidu_token");
			SEARCH_BEGINTIME = property.getProperty("search_beginTime");
			SEARCH_ENDTIME = property.getProperty("search_endTime");
			BAIDU_SAVE = property.getProperty("baidu_save_dst");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
3、配置文件======

baidu_manager=XX
baidu_manager_passwd=XX
baidu_sub_account=账号
baidu_token=XX
baidu_save_dst=E:\\路径
search_beginTime=2017-01-01
search_endTime=2017-02-28

4、HttpUtil========

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class HttpUtil {
	private static CloseableHttpClient httpClient = null;
	static {
		// 获取一个socket的连接工厂,可以设置一些参数。
		ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
		LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
		Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
				.register("http", plainsf).register("https", sslsf).build();
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
		cm.setMaxTotal(1000); // 这个是最大连接数。如果并发量大于这个数,就会发生排队。
		cm.setDefaultMaxPerRoute(1000); // 设置每个路由的最大值。
		// 根据上面的一堆配置,生成一个client对象。 这个对象是整个应用程序公用的。是唯一的对象。
		httpClient = HttpClients.custom().setConnectionManager(cm).build();
	}

	/**
	 * post请求
	 * 
	 * @param url
	 * @param requestBody
	 * @return 返回内容
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String post(String url, String requestBody) throws ClientProtocolException, IOException {
		// 创建一个post请求对象
		HttpPost postRequest = new HttpPost(url);
		StringEntity input = new StringEntity(requestBody, Charset.forName("utf-8"));
		input.setContentType("application/json");
		postRequest.setEntity(input);
		CloseableHttpResponse response = httpClient.execute(postRequest);

		String str = null;
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instreams = entity.getContent();
			str = inputStream2String(instreams);
		}
		response.close();
		return str;
	}

	public static String post(String url, List<NameValuePair> params) throws ClientProtocolException, IOException {
		// 创建一个post请求对象
		HttpPost postRequest = new HttpPost(url);

		UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(params, "utf-8");
		postRequest.setEntity(formEntiry);
		CloseableHttpResponse response = httpClient.execute(postRequest);

		String str = null;
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instreams = entity.getContent();
			str = inputStream2String(instreams);
		}
		response.close();
		return str;
	}

	/**
	 * get请求
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	// http://www.baidu.com?param1=value1¶m2=value2
	public static String get(String url) throws ClientProtocolException, IOException {
		// 创建一个get请求对象
		HttpGet getRequest = new HttpGet(url);
		HttpResponse response = httpClient.execute(getRequest);
		String str = null;
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instreams = entity.getContent();
			str = inputStream2String(instreams);
		}
		return str;
	}

	public static InputStream getInputStream(String url) throws ClientProtocolException, IOException {
		// 创建一个get请求对象
		HttpGet getRequest = new HttpGet(url);
		HttpResponse response = httpClient.execute(getRequest);
		String str = null;
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instreams = entity.getContent();
			return instreams;
		}
		return null;
	}

	/**
	 * 从网络输入流中获取数据
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	private static String inputStream2String(InputStream in) throws IOException {
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			out.append(new String(b, 0, n));
		}
		in.close();
		return out.toString();
	}

	public static void inputStream2File(InputStream in, String dst) throws IOException {
		FileOutputStream fo = new FileOutputStream(dst);
		
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			fo.write(b, 0, n);
		}
		in.close();
		fo.flush();
		fo.close();
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值