当前位置:网站首页>How to integrate Google APIs with Google's application system (3) -- call the restful service of Google discovery API
How to integrate Google APIs with Google's application system (3) -- call the restful service of Google discovery API
2022-07-29 09:59:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I wish every programmer can learn more languages .
Said so much , Then first of all, agree with me with Google Discovery RESTful Service as an example , Show you how to use the most common Java Code calls Google Discovery RESTful service .
introduction :
stay “ How to use Google APIs and Google Application system integration (2)” Of , I listed the current Google APIs All supported Google APIs. In fact, I call this table with code Google Discovery RESTFul The service is self generated . The detailed steps and codes are as follows :
(1) Visit Google Discovery RESTFul Service for :https://www.proxy.ustclug.org/discovery/v1/apis To be able to obtain RESTFul The result returned by the service : By visiting JSONtoStringConverter–>readJSONSAsString()
package com.henry.json.gson.googlediscovery;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JSONtoStringConverter {
private String url_path="https://www.proxy.ustclug.org/discovery/v1/apis";
public String readJSONSAsString(){
InputStream in=this.getJSONSchemaInputStream();
return readJSONSAsString(in);
}
private InputStream getJSONSchemaInputStream() {
InputStream ipStream = null;
if (url_path == null) {
throw new IllegalArgumentException("The URL Path can't be empty!!!");
}
try {
URL url = new URL(url_path);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setReadTimeout(30000);
httpConnection.setDoInput(true);
ipStream = httpConnection.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ipStream;
}
private String readJSONSAsString(InputStream in){
String jsonString="";
ByteArrayOutputStream baosArrayOutputStream=new ByteArrayOutputStream();
byte[] bytes=new byte[1024];
int len=0;
try {
while((len=in.read(bytes))!=-1){
baosArrayOutputStream.write(bytes, 0, len);
}
jsonString=new String(baosArrayOutputStream.toByteArray(),"utf-8");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(jsonString);
return jsonString;
}
public static void main(String[] args) {
JSONtoStringConverter jSONtoStringConverter=new JSONtoStringConverter();
jSONtoStringConverter.readJSONSAsString();
}
}(2) Parse the returned JSON data , But analysis once , We need to establish corresponding JavaBean, So we can take JSON The object and Java Object mapping .
2.1 GoogleDiscoveryBean
package com.henry.json.gson.googlediscovery;
import java.util.List;
public class GoogleDiscoveryBean {
private String kind;
private String discoveryVersion;
private List<Items> items;
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getDiscoveryVersion() {
return discoveryVersion;
}
public void setDiscoveryVersion(String discoveryVersion) {
this.discoveryVersion = discoveryVersion;
}
public List<Items> getItems() {
return items;
}
public void setItems(List<Items> items) {
this.items = items;
}
@Override
public String toString(){
return kind+"--"+discoveryVersion+"--size:"+items.size();
}
}2.2 Items
package com.henry.json.gson.googlediscovery;
/*
"kind": "discovery#directoryItem",
"id": "adexchangebuyer:v1",
"name": "adexchangebuyer",
"version": "v1",
"title": "Ad Exchange Buyer API",
"description": "Lets you manage your Ad Exchange Buyer account.",
"discoveryRestUrl": "https://www.proxy.ustclug.org/discovery/v1/apis/adexchangebuyer/v1/rest",
"discoveryLink": "./apis/adexchangebuyer/v1/rest",
"icons": {
"x16": "http://www.google.com/images/icons/product/doubleclick-16.gif",
"x32": "http://www.google.com/images/icons/product/doubleclick-32.gif"
},
"documentationLink": "https://developers.google.com/ad-exchange/buyer-rest",
"preferred": false
*/
public class Items {
private String kind;
private String id;
private String name;
private String version;
private String title;
private String description;
private String discoveryRestUrl;
private String discoveryLink;
private String documentationLink;
private String preferred;
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscoveryRestUrl() {
return discoveryRestUrl;
}
public void setDiscoveryRestUrl(String discoveryRestUrl) {
this.discoveryRestUrl = discoveryRestUrl;
}
public String getDiscoveryLink() {
return discoveryLink;
}
public void setDiscoveryLink(String discoveryLink) {
this.discoveryLink = discoveryLink;
}
public String getDocumentationLink() {
return documentationLink;
}
public void setDocumentationLink(String documentationLink) {
this.documentationLink = documentationLink;
}
public String getPreferred() {
return preferred;
}
public void setPreferred(String preferred) {
this.preferred = preferred;
}
}(3) download JSON java The library of : http://code.google.com/p/google-gson/ GSon It's an analysis provided by Google JSON data : 1. Google GSON This Java The class library can Java Object conversion to JSON, Can also put JSON The string is converted to an equal Java object . 2.Gson Support random and complex Java Objects contain objects without source code .
(4) Create a GoogleGSonTools: This class will Google Discovery RESTful Service returned JSON String , Take the initiative to transform into GoogleDiscoveryBean object , This method cannot 10 That's ok , It's that simple .
package com.henry.json.gson.googlediscovery;
import com.google.gson.Gson;
public class GoogleGSonTools {
public static <T> T getGoogleDiscoveryBean(String josnString, Class<T> clazz) {
T t = null;
try {
Gson gson = new Gson();
t = gson.fromJson(josnString, clazz);
} catch (Exception e) {
}
return t;
}
}(5) Combine the above (1)~(4), We return the value , Format into a HTML Table for .
package com.henry.json.gson.googlediscovery;
import java.util.List;
public class GoogleAPIsListViewService {
public String listAllGoogleAPIs(){
StringBuilder sbBuilder=new StringBuilder("<table border=\"1\" style=\"word-break:break-all; word-wrap:break-word;\"> <tr><td> Serial number </td><td>API title </td><td> name </td><td> Version number </td><td>RestFul Requested URL</td><td>RestFul Requested URL</td></tr>");
JSONtoStringConverter jSONtoStringConverter=new JSONtoStringConverter();
String json=jSONtoStringConverter.readJSONSAsString();
GoogleDiscoveryBean googleDiscoveryBean=GoogleGSonTools.getGoogleDiscoveryBean(json, GoogleDiscoveryBean.class);
List<Items> listItems=googleDiscoveryBean.getItems();
if(listItems!=null&&listItems.size()>0){
for(int i=0;i<listItems.size();i++){
Items items=listItems.get(i);
sbBuilder.append("<tr>");
sbBuilder.append("<td>").append(" "+(i+1)+" ").append("</td>");
sbBuilder.append("<td>").append(items.getTitle()).append("</td>");
sbBuilder.append("<td>").append(items.getName()).append("</td>");
sbBuilder.append("<td>").append(items.getVersion()).append("</td>");
sbBuilder.append("<td>").append(items.getDiscoveryRestUrl()).append("</td>");
sbBuilder.append("<td>").append(items.getDocumentationLink()).append("</td>");
sbBuilder.append("</tr>");
}
}
sbBuilder.append("</table>");
System.out.println(sbBuilder.toString());
return sbBuilder.toString();
}
public static void main(String[] args) {
GoogleAPIsListViewService gavs=new GoogleAPIsListViewService();
gavs.listAllGoogleAPIs();
}
}The output is zero : “ How to use Google APIs and Google Application system integration (2)” Of the table seen in the article html Source code .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/118859.html Link to the original text :https://javaforall.cn
边栏推荐
- Explanation of C value type and reference type
- JS to achieve full screen effect
- 机器学习之线性回归(最小二乘法手写+sklearn实现)
- 2021年CS保研经历(六):系统填报 + 一些感想
- 还是有机会的
- 《LOL》从代码上来说最难的是哪个英雄?
- 2021年CS保研经历(四):西交软院预推免、信工所三室预推免
- Virtual machines use host graphics cards (Hyper-V and wsl2)
- Orbslam2 installation test and summary of various problems
- [Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
猜你喜欢

Encyclopedia of introduction to machine learning - 2018 "machine learning beginners" official account article summary

pytest+allure生成测试报告

MySQL infrastructure: SQL query statement execution process

Sample is new and supported from API 8! Come and take it

How can Plato obtain premium income through elephant swap in a bear market?

English语法_不定代词 - 常用短语

数据库表结构生成excel工具
![[ts]typescript learning record pit collection](/img/4c/14991ea612de8d5c94b758174a1c26.png)
[ts]typescript learning record pit collection

Implementation and verification logic of complex expression input component

【AAAI】用于交通流预测的基于注意力的时空图卷积网络
随机推荐
Soft exam summary
MySQL infrastructure: SQL query statement execution process
【C语言】扫雷(递归展开 + 标记功能)
English语法_不定代词 - 常用短语
Some suggestions for programmers to leave single
待人宽容大度
机器学习之线性回归(最小二乘法手写+sklearn实现)
尹伊:我的学习成长路径
Sublime Text3 设置不同文件不同缩进
Div horizontal layout aligned on both sides
Briefly describe the difference between heap and stack
In simple terms, dependency injection and its application in Tiktok live broadcast
Harmonyos 3.0 release!
程序员脱离单身的一些建议
Gao Zhiwei: data management enables the digital transformation of the transportation industry
Visual Studio
Functions and arrays
Logistic regression of machine learning
Unity xchart3.0 basic usage quick start
阿左的境界