How to integrate with Google APIs and Google Apps (3)----call Google Discovery RESTful services

Source: Internet
Author: User

Having said so much, let me first take Google Discovery RESTful services as an example to show you how to call Google Discovery RESTful services with the most common Java code.

Introduction:

Under "How to integrate with Google APIs and Google Apps (2)", I've listed all of the Google APIs that are currently supported by Google APIs. In fact, this form was generated automatically by calling Google Discovery RESTful services in code. The specific steps and code are as follows:

(1) Access to Google Discovery RESTful services: Https://www.googleapis.com/discovery/v1/apis can get the results returned by RESTful services: by accessing 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 ";p ublic String readjsonsasstring () {InputStream in=this.getjsonschemainputstream (); return readjsonsasstring (in );} Private InputStream Getjsonschemainputstream () {InputStream IPStream = null;if (Url_path = = null) {throw new Illegalargume Ntexception ("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 before parsing, we need to establish the corresponding JavaBean so that the JSON object and the Java object can be mapped together.

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;} @Overridepublic 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 Buye R 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) Download the JSON Java library: http://code.google.com/p/google-gson/
Gson is the official parsing JSON data provided by Google:
1. Google Gson this Java class library can convert a Java object to JSON, or it can convert a JSON string into an equal Java object.
2.Gson supports any complex Java object, including objects without source code.

(4) Create a googlegsontools: This class will automatically convert the JSON string returned by the Google Discovery restful service to the Googlediscoverybean object, which is as simple as 10 lines.

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 = GS On.fromjson (josnstring, clazz);} catch (Exception e) {}return t;}}

(5) Combining the above (1) ~ (4), we format the returned value into an HTML table.

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 </td><td>api title </td><td> name </td> <td> version </td><td>restful requested url</td></tr> ") of the url</td><td>restful request; 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 result of the output is the HTML source code for the table you see in the article "How to integrate with Google APIs and Google's applications (2)".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.