博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
百度广告联盟api probuf协议对接
阅读量:6512 次
发布时间:2019-06-24

本文共 4055 字,大约阅读时间需要 13 分钟。

百度的广告API使用的是不是通常的http协议,而是使用谷歌开源出来的probuf协议,具体介绍请参考:

 

我们对接的客户端使用的是java,所以先会引用相对的jar包,使用的maven,如下:

com.google.protobuf
protobuf-java
2.5.0
org.apache.httpcomponents
httpclient
4.5.2
com.googlecode.protobuf-java-format
protobuf-java-format
1.2

  

需要在windows下载probuf的exe文件,来根据协议生成java的实体class

在这里下载

 

执行脚本来根据百度提供的 aidu_mobads_api_5.proto 文件生成java类

protoc.exe --java_out=./ baidu_mobads_api_5.proto

 

会生成一个这个:

 

然后使用的时候大概是这样:  (会注释掉敏感的)

BaiduMobadsApi5.App.Builder appBuilder = BaiduMobadsApi5.App.newBuilder();		BaiduMobadsApi5.Version.Builder appversionBuilder = BaiduMobadsApi5.Version.newBuilder();		appversionBuilder.setMajor(3);		appversionBuilder.setMinor(2);		appversionBuilder.setMicro(0);		appBuilder.setAppId("");		appBuilder.setAppPackage("");		appBuilder.setAppVersion(appversionBuilder);						BaiduMobadsApi5.AdSlot.Builder adSlotBuilder = BaiduMobadsApi5.AdSlot.newBuilder();		BaiduMobadsApi5.Size.Builder sizeBuilder = BaiduMobadsApi5.Size.newBuilder();		sizeBuilder.setWidth(580);		sizeBuilder.setHeight(90);		adSlotBuilder.setAdslotId("");		adSlotBuilder.setAdslotSize(sizeBuilder);		BaiduMobadsApi5.Device.Builder deviceBuilder = BaiduMobadsApi5.Device.newBuilder();				deviceBuilder.setDeviceType(BaiduMobadsApi5.Device.DeviceType.PHONE);		deviceBuilder.setOsType(OsType.ANDROID);		BaiduMobadsApi5.Version.Builder osversionBuilder = BaiduMobadsApi5.Version.newBuilder();		osversionBuilder.setMajor(8);		osversionBuilder.setMinor(1);		osversionBuilder.setMicro(0);				deviceBuilder.setOsVersion(osversionBuilder);					deviceBuilder.setVendor(ByteString.copyFrom("OPPO".getBytes()));		deviceBuilder.setModel(ByteString.copyFrom("PADM00".getBytes()));		BaiduMobadsApi5.Size.Builder screensizeBuilder = BaiduMobadsApi5.Size.newBuilder();		screensizeBuilder.setWidth(800);		screensizeBuilder.setHeight(600);						deviceBuilder.setScreenSize(screensizeBuilder);						BaiduMobadsApi5.UdId.Builder udidBuilder = BaiduMobadsApi5.UdId.newBuilder();		udidBuilder.setImei("");		udidBuilder.setAndroidId("");		udidBuilder.setMac("");		deviceBuilder.setUdid(udidBuilder);					BaiduMobadsApi5.Network.Builder networkBuilder = BaiduMobadsApi5.Network.newBuilder();		networkBuilder.setIpv4("");		networkBuilder.setConnectionType(ConnectionType.CELL_4G);		networkBuilder.setOperatorType(OperatorType.CHINA_MOBILE);						String requestId ="";		BaiduMobadsApi5.Version.Builder versionBuilder = BaiduMobadsApi5.Version.newBuilder();		versionBuilder.setMajor(5);		versionBuilder.setMinor(5);		versionBuilder.setMicro(0);				MobadsRequest mobadsRequest = MobadsRequest.newBuilder().setRequestId(requestId).setApiVersion(versionBuilder)				.setApp(appBuilder).setAdslot(adSlotBuilder).setDevice(deviceBuilder).setNetwork(networkBuilder).build();				byte[] content = mobadsRequest.toByteArray();  				BaiduMobadsApi5.MobadsRequest request = BaiduMobadsApi5.MobadsRequest.parseFrom(content);		System.out.println(request.getApp().getAppId());				System.out.println(mobadsRequest.getApp().getAppId());						HttpPost httpPost = new HttpPost("");		httpPost.setEntity(new ByteArrayEntity(content));		httpPost.setHeader("Content-type", "application/octet-stream;charset=utf-8");		CloseableHttpClient m_HttpClient = HttpClients.createDefault();				CloseableHttpResponse httpResponse = m_HttpClient.execute(httpPost);				try {			HttpEntity entityResponse = httpResponse.getEntity();					BaiduMobadsApi5.MobadsResponse response = BaiduMobadsApi5.MobadsResponse.parseFrom(entityResponse.getContent());//			System.out.println(response.getErrorCode());//			System.out.println(response.getRequestId());//			System.out.println(response.getAdsList().get(0).getMaterialMeta().getImageSrc(0));						String jsonFormat =JsonFormat.printToString(response);						System.out.println(jsonFormat);					} finally {			httpResponse.close();		}

  

 

 

 

 

 

 

 

转载地址:http://iisfo.baihongyu.com/

你可能感兴趣的文章
android动画
查看>>
新书试读_信息系统项目管理师考试考点分析与真题详解
查看>>
LVS Nginx HAProxy 优缺点
查看>>
Android开发之开源框架OKHTTP的Get请求代码,得到json字符串方法
查看>>
images对象实现图片幻灯片
查看>>
Oracle 12c 日常维护
查看>>
CF 445A DZY Loves Chessboard
查看>>
Cobbler简介
查看>>
恢复 git reset -hard 的误操作
查看>>
selected
查看>>
菜鸟初始代码旅程——修改记录
查看>>
C# WinForm 文件上传下载
查看>>
【javascript】ajax请求 编码问题导致的ie浏览器在输入中文文字后没有内容,而chrome正常搜到文字...
查看>>
Git分支操作
查看>>
2.C#自定义Attribute
查看>>
最近一周学习总结
查看>>
选中单选按钮后显示其他表单元素
查看>>
编写java程序,添加注释,生成您的javadoc api文件
查看>>
Android 高仿UC浏览器监控剪切板弹出悬浮窗功能
查看>>
使用axios向后端传递数据,后端接收不到?
查看>>