Using Google's Web service

Source: Internet
Author: User
Tags soap query tostring valid xmlns email account
Google|web One, Introduction
The Google search engine provides a SOAP based Web Service. This means that this service can be used in different development languages and development environments, and that Google, in order to simplify Java programmer Development, also provides a set of Java API interfaces for accessing Web Serivce, making it easy to develop a program that supports Google's search functionality. Developers can embed Google's search capabilities into their applications. This article describes how to use these Java APIs and how to use Google's Web service.
At present, Google's API is still in beta testing phase, so there are some restrictions on the use of the API. For example, use these APIs to request an account number. For the account of the free application, in order to prevent the improper use of the developer, limit each account, can only inquire 1000 times per day. At present, Google has not proposed a formal mode of operation and charging methods.
Google API currently provides three types of services, respectively, as follows:
1) Search service. Users submit a search content request, Google Server will look for more than 2 billion of the content of the Web page, and will meet the needs of users to return to the user, in general, this process only takes a few seconds.
2 Caching Service (cache). The user submits a Url,google server that returns the latest record of the searcher's access to the URL. This makes it easy for users to reuse Google's search results.
3) spell check. This feature is used to check whether a user's query request is valid. The user submits a check content, the Google server will return a valid, closest to the content of the query request, the returned query request will conform to Google's query rules.

Ii. Related preparatory work
In order to develop a java-based program, we need to prepare for the following tasks.
1 Create Java Development environment, Java SDK 1.3.1 or newer version. Related Address Http:.//java.sun.com/j2se
2 Download the Google API jar file, Googleapi.jar. Developers can download to Http://www.google.com/apis.
3 Apply for Google Access account, the current Google support for free applications, users need to use an email account to obtain a new account. Note that the current free account supports the maximum number of 1000 queries per day. Application Address: Http://www.google.com/apis.
4) (optional) If a user needs to send a SOAP request directly through a Java program, the developer needs to download the relevant Java software package, JAXM. Download Address: Http://java.sun.com/xml

Iii. Use of Soap
Google provides soap based Web Service, so users can submit SOAP query requests to Google servers, and Google servers will process those requests and return the results of the query in SOAP format. Here is a query request and query result.
Query: For example, there is a query request, the request type is search, the content of the query is "World Cup", the account number is "123456789", the following is an example of the query.
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<soap-env:envelope
xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"
Xmlns:xsi= "Http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd= "Http://www.w3.org/1999/XMLSchema" >
<SOAP-ENV:Body>
<ns1:dogooglesearch xmlns:ns1= "Urn:googlesearch"
soap-env:encodingstyle=
"http://schemas.xmlsoap.org/soap/encoding/" >
<key xsi:type= "Xsd:string" >123456789</key>
<q xsi:type= "xsd:string" > "World Cup" </q>
<start xsi:type= "Xsd:int" >0</start>
<maxresults xsi:type= "Xsd:int" >10</maxResults>
<filter xsi:type= "Xsd:boolean" >true</filter>
<restrict xsi:type= "Xsd:string" ></restrict>
<safesearch xsi:type= "Xsd:boolean" >false</safeSearch>
&LT;LR xsi:type= "Xsd:string" ></lr>
<ie xsi:type= "Xsd:string" >latin1</ie>
<oe xsi:type= "Xsd:string" >latin1</oe>
</ns1:dogooglesearch >
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Query results: If we execute the above query, we can get the following query results. Among them, the query result has a total of about 2660000 records, the use time is 0.125012 seconds. The item tag represents a query result, and in the first query result, the URL for the Web site is http://www.fifaworldcup.com. This example simply lists the results of a query.

<?xml version= "1.0" encoding= "UTF-8"?>
<soap-env:envelope xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"
Xmlns:xsi= "Http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd= "Http://www.w3.org/1999/XMLSchema" >
<SOAP-ENV:Body>
<ns1:dogooglesearchresponse
xmlns:ns1= "Urn:googlesearch"
soap-env:encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/" >
<return xsi:type= "Ns1:googlesearchresult" >
<documentfiltering xsi:type= "Xsd:boolean" >false</documentFiltering>
<estimatedtotalresultscount xsi:type= "Xsd:int" >
2660000</estimatedtotalresultscount>
<directorycategories
Xmlns:ns2= "http://schemas.xmlsoap.org/soap/encoding/"
Xsi:type= "Ns2:array"
Ns2:arraytype= "Ns1:directorycategory[0]" >
</directoryCategories>
<searchtime xsi:type= "Xsd:double" >0.125012</searchTime>
<resultelements
xmlns:ns3= "http://schemas.xmlsoap.org/soap/encoding/"
Xsi:type= "Ns3:array" ns3:arraytype= "ns1:resultelement[10]" >

<item xsi:type= "Ns1:resultelement" >
<cachedsize xsi:type= "Xsd:string" >10k</cachedSize>
<snippet xsi:type= "Xsd:string" ></snippet>
<directorycategory xsi:type= "Ns1:directorycategory" >
<specialencoding xsi:type= "xsd:string"/>
</directoryCategory>
<relatedinformationpresent xsi:type= "Xsd:boolean" >
True
</relatedInformationPresent>
<summary xsi:type= "Xsd:string" >
The official site from FIFA, made by Yahoo. Daily news updates and loads of relevant information.
</summary>
<url xsi:type= "Xsd:string" >
Http://www.fifaworldcup.com
</URL>
<title xsi:type= "Xsd:string" >
2002 FIFA <b>World</b>b>Cup</b> (tm)
</title>
</item>
...
</resultElements>
<endindex xsi:type= "Xsd:int" >10</endIndex>
<searchtips xsi:type= "xsd:string"/>
<searchcomments xsi:type= "xsd:string"/>
<startindex xsi:type= "Xsd:int" >1</startIndex>
<estimateisexact xsi:type= "Xsd:boolean" >false</estimateIsExact>
<searchquery xsi:type= "Xsd:string" >
"Science Fiction"
</searchQuery>
</return>
</ns1:doGoogleSearchResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Iv. using the Google API
To improve developer productivity, Google provides a Java-based API for direct access to Google servers. This API wraps up Google's Web service and is more convenient to use than Web service. The API package mainly includes the following classes:
Googlesearch: This class encapsulates access to Google servers and supports search and caching (cache) functionality.
Googlesearchdirectorycategory: Represents a category directory in Google
Googlesearchfault: This class is a subclass of exception, which is used to indicate an error that the API uses.
Googlesearchresult: This class encapsulates the results of the search.
Googlesearchresultelement: This class represents each record in the search results.

The following is an example of the source code, for a simple query, query request for "World Cup", the query account number is "123456789". This example will print out the results of the query. You can use the Googlesearchresult and Googlesearchresultelement classes if the user needs to resolve the query results in a near-step.

Import com.google.soap.search.*;
Import java.io.*;

public class Googleapitest {

public static void Main (string[] args) {

String clientkey= "123456789";
String query= "Word Cup";
Create a Google Search object, set our authorization key
Googlesearch s = new Googlesearch ();
S.setkey (Clientkey);
try {
s.setquerystring (query);
Googlesearchresult r = S.dosearch ();
System.out.println ("Google Search Results:" +t.tostring ());
catch (Googlesearchfault f) {
SYSTEM.OUT.PRINTLN ("The call to the Google Web APIs failed:" +f.tostring ());
}
}
}

V. Summary
Google search engine provides the search, caching and spelling simple features, through the Web service can be used on different platforms, different languages, in order to simplify the development of soap, Google also provides a package of SOAP services Java API, This has also greatly improved the development efficiency of Java programmers. With the search content, the form of rich, more developers can use this technology in their own applications, expand the level of application capabilities.


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.