I. Introduction
Google search engine provides a soap-based Web service. This means that this service can be used in different development languages and environments. In addition, to simplify Java programmer development, Google also provides a set of Java API interfaces for accessing Web Serivce, this makes it easy to develop a program that supports the Google search function. Developers can embed Google's search functions into their applications. This article describes how to use these Java APIs and how to use Google's web service.
Currently, Google's API is still in beta testing, so there are still some restrictions on the use of the API. For example, you need to apply for an account to use these Apis. For free accounts, to prevent improper use by developers, each account can be queried up to 1000 times a day. At present, Google has not yet proposed a formal operation model and billing method.
Google APIs currently provide the following three services:
1) search service. When a user submits a search content request, the Google server searches for more than 2 billion of the webpage content and returns the target meeting the user's requirements to the user. Generally, this processing takes only a few seconds.
2) cache service ). When a user submits a URL, the Google server returns the latest record of the searcher accessing the URL. In this way, users can easily reuse Google's search results.
3) check spelling. This function is used to check whether a user's query request is valid. When a user submits a check content, the Google server returns a valid query Request closest to the content, and the returned query request will comply with Google's query rules.
2. Preparations
To develop Java-based programs, we need to make the following preparations.
1) create a Java development environment, Java SDK 1.3.1 or an updated version. Related address: http:. // java.sun.com/j2se
2) download the Google api jar file, googleapi. jar. Developers can download it at http://www.google.com/apis.
3) apply for a Google access account. Currently, Google supports free application. You need to use an email account to obtain a new account. Note: currently, the free account supports a maximum of 1000 queries per day. Application address: http://www.google.com/apis.
4) (optional) If You Need To directly send SOAP requests through Java programs, developers need to download the relevant Java software package jaxm. : Http://java.sun.com/xml
Iii. Use of soap
Google provides a soap-based Web Service. Therefore, you can submit soap query requests to the Google server. Then, the Google server processes these requests and returns the query results in the soap format. The following is a query request and query result.
Query: for example, there is a query request. The request type is search, the query content is "World Cup", and the account is "123456789". The following is an example of this 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>
<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 preceding query, we can get the following query results. The query results contain a total of about 2660000 records, and the query time is 0.125012 seconds. The item tag represents a query result. In the first query result, the URL of the website is a http://www.fifaworldcup.com. This example only lists one query result.
<? 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: int32 = "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"> 10 k </cachedsize>
<Hostname xsi: TYPE = "XSD: string"/>
<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> 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>
4. Use Google APIs
To improve the development efficiency of developers, Google provides a set of Java-based APIs for direct access to Google servers. This API encapsulates Google's Web service, making it easier to use than web service. This API package mainly includes the following categories:
Googlesearch: This class encapsulates access to Google servers and supports search and cache functions.
Googlesearchdirectorycategory: A category directory in Google
Googlesearchfault: a subclass of exception, used to indicate API usage errors.
Googlesearchresult: This class encapsulates the search results.
Googlesearchresultelement: This class indicates each record in the search result.
The following is the source code of an example for a simple query. The query request is "World Cup" and the query account is "123456789 ". This example prints the query results. You can use the googlesearchresult and googlesearchresultelement classes to parse the query results.
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 simple search, cache, and spelling functions. Web services can be used on different platforms and languages. To simplify soap development, google also provides Java APIs packaged with the soap service, which greatly improves the development efficiency of Java programmers. With the rich search content and forms, more developers can use this technology in their own applications to expand the application level.