说了这么多,那么首先允许我以Google Discovery RESTful服务为例,给大家演示如何用最普通的Java代码调用Google Discovery RESTful服务。
引言:
在“如何用Google APIs和Google的应用系统进行集成(2)”的下面,我列出了当前Google APIs支持的所有的Google APIs。其实这个表格是我用代码调用Google Discovery RESTFul服务自动生成的。具体的步骤和代码如下:
(1) 访问Google Discovery RESTFul的服务:https://www.googleapis.com/discovery/v1/apis 可以获得RESTFul服务返回的结果:通过访问 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.googleapis.com/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) 解析返回的JSON数据,但是解析以前,我们需要建立相应的JavaBean,这样就能把JSON的对象和Java的对象映射起来。
2.1GoogleDiscoveryBean
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.googleapis.com/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) 下载JSON java的库:http://code.google.com/p/google-gson/
GSon是谷歌官方提供的解析JSON数据:
1.谷歌GSON这个Java类库可以把Java对象转换成JSON,也可以把JSON字符串转换成一个相等的Java对象。
2.Gson支持任意复杂Java对象包括没有源代码的对象。
(4) 创建一个GoogleGSonTools: 这个类会把Google Discovery RESTful服务返回的JSON的字符串,自动转换成GoogleDiscoveryBean对象,这个方法不到10行,就这么简单。
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)结合上面的(1)~(4),我们把其返回的值,格式化成一个HTML的表格。
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>序号</td><td>API 标题</td><td>名字</td><td>版本</td><td>RestFul请求的URL</td><td>RestFul请求的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();
}
}
输出的结果就是: “
如何用Google APIs和Google的应用系统进行集成(2)”一文中看到的表格的html源代码。
分享到:
相关推荐
Api-securing-restful-apis-with-jwt.zip,如何使用json web令牌保护nodejs restful crud api?用jwt保护restfulapi,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库...
Metamorphic-Testing-of-RESTful-Web-APIs.pdf
google 删除了的addon-google_apis_x86-google-19
赠送jar包:jakarta.xml.bind-api-2.3.3.jar; 赠送原API文档:jakarta.xml.bind-api-2.3.3-javadoc.jar; 赠送源代码:jakarta.xml.bind...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
dropwizard-swagger.zip,一种在dropwizard中使用swagger a dropwizard包来记录restapi的简单方法,该包提供swagger ui静态内容并加载swagger端点。
addon-google_apis-google-24
google api 19
You’ll explore the concepts behind REST, learn different strategies for creating hypermedia-based APIs, and then put everything together with a step-by-step guide to designing a RESTful Web API.
android的普通安装方法速度很慢,对一般的用户要数小时的时间...google_apis-8_r02.part3.rar google_apis-8_r02.part4.rar 也可到以下地址下载: http://dl-ssl.google.com/android/repository/google_apis-8_r02.zip
android系统源码下载工具repo,下载源:https://storage.googleapis.com/git-repo-downloads/repo
资源分类:Python库 所属语言:Python 使用前提:需要解压 资源全名:googleapis_common_protos-1.52.0-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
googleapis, Google api的public 接口定义 Google api这个库包含支持REST和gRPC协议的public api原始接口定义。 阅读原始界面定义可以更好地理解 Google api并帮助你更高效地利用它们。 你还可以将这些定义与开源...
google 删除了的addon-google_apis_x86-google-19
google-api-python-client:Python 用的 Google APIs 客户端库关于 这是用于 Google 基于发现的 API 的 Python 客户端库。 要开始使用,请参阅此库的完整文档。 此外,动态生成的文档可用于此库支持的所有 API。 ...
原地址:http://commondatastorage.googleapis.com/git-repo-downloads/repo,不方便的可以在这里下载
google_apis-3-r03google_apis-3-r03google_apis-3-r03google_apis-3-r03
API允许应用程序访问数据并与外部软件组件,操作系统或微服务进行交互。 为简化起见,API 将用户响应传送到系统并将系统响应发送回用户。 关于 RESTful APIs- REST 代表 REpresentational State Transfer。 它是...
Discover the RESTful technologies, including REST, JSON, XML, JAX-RS web services, SOAP and more, for building today's microservices, big data applications, and web service applications. This book is ...
| <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900"> | <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons">