如何用Google APIs和Google的应用系统进行集成(3)----调用Google Discovery RESTful服务
2020-12-13 02:22
标签:google apis restful服 调用restful gson java http 说了这么多,那么首先允许我以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() (2) 解析返回的JSON数据,但是解析以前,我们需要建立相应的JavaBean,这样就能把JSON的对象和Java的对象映射起来。 2.1 GoogleDiscoveryBean 2.2 Items
(4) 创建一个GoogleGSonTools: 这个类会把Google Discovery RESTful服务返回的JSON的字符串,自动转换成GoogleDiscoveryBean对象,这个方法不到10行,就这么简单。 如何用Google APIs和Google的应用系统进行集成(3)----调用Google Discovery RESTful服务,搜素材,soscw.com 如何用Google APIs和Google的应用系统进行集成(3)----调用Google Discovery RESTful服务 标签:google apis restful服 调用restful gson java http 原文地址:http://blog.csdn.net/chancein007/article/details/28104355package 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();
}
}
package com.henry.json.gson.googlediscovery;
import java.util.List;
public class GoogleDiscoveryBean {
private String kind;
private String discoveryVersion;
private List
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对象包括没有源代码的对象。package com.henry.json.gson.googlediscovery;
import com.google.gson.Gson;
public class GoogleGSonTools {
public static
(5)结合上面的(1)~(4),我们把其返回的值,格式化成一个HTML的表格。
package com.henry.json.gson.googlediscovery;
import java.util.List;
public class GoogleAPIsListViewService {
public String listAllGoogleAPIs(){
StringBuilder sbBuilder=new StringBuilder("
");
System.out.println(sbBuilder.toString());
return sbBuilder.toString();
}
public static void main(String[] args) {
GoogleAPIsListViewService gavs=new GoogleAPIsListViewService();
gavs.listAllGoogleAPIs();
}
}
");
JSONtoStringConverter jSONtoStringConverter=new JSONtoStringConverter();
String json=jSONtoStringConverter.readJSONSAsString();
GoogleDiscoveryBean googleDiscoveryBean=GoogleGSonTools.getGoogleDiscoveryBean(json, GoogleDiscoveryBean.class);
List序号
API 标题
名字
版本
RestFul请求的URL
RestFul请求的URL
").append(" "+(i+1)+" ").append(" ");
sbBuilder.append("").append(items.getTitle()).append(" ");
sbBuilder.append("").append(items.getName()).append(" ");
sbBuilder.append("").append(items.getVersion()).append(" ");
sbBuilder.append("").append(items.getDiscoveryRestUrl()).append(" ");
sbBuilder.append("").append(items.getDocumentationLink()).append(" ");
sbBuilder.append("");
}
}
sbBuilder.append("
输出的结果就是,就是“如何用Google APIs和Google的应用系统进行集成(2)”文章中看到的表格的html源代码。
文章标题:如何用Google APIs和Google的应用系统进行集成(3)----调用Google Discovery RESTful服务
文章链接:http://soscw.com/essay/25452.html