百度的廣告API使用的是否是一般的http協議,而是使用谷歌開源出來的probuf協議,具體介紹請參考:https://www.jianshu.com/p/b1f18240f0c7https://www.jianshu.com/p/b1f18240f0c7java
咱們對接的客戶端使用的是java,因此先會引用相對的jar包,使用的maven,以下:apache
<dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>com.googlecode.protobuf-java-format</groupId> <artifactId>protobuf-java-format</artifactId> <version>1.2</version> </dependency>
須要在windows下載probuf的exe文件,來根據協議生成java的實體classjson
https://download.csdn.net/download/can007/5816063 在這裏下載windows
執行腳原本根據百度提供的 aidu_mobads_api_5.proto 文件生成java類api
protoc.exe --java_out=./ baidu_mobads_api_5.proto
會生成一個這個:app
而後使用的時候大概是這樣: (會註釋掉敏感的)maven
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(); }