日期:2012-10-10  浏览次数:20609 次

一、简介
Google搜索引擎提供了基于SOAP的Web Service。这意味着不同的开发语言、开发环境都能够使用这种服务,另外,Google为了简化Java程序员的开发,它还提供了一套Java API接口,用于访问Web Serivce,这使得开发一套支持Google搜索功能的程序变得十分容易。开发人员可以将Google的搜索功能嵌入到他们的应用程序当中。本文将介绍如何使用这些Java API,以及如何使用google的Web Service。
目前,Google的API还是处于Beta测试阶段,因此,在API的使用上还有一些限制。例如,使用这些API需要申请一个帐号。对于免费申请的帐号,为了防止开发人员不正当的使用,限制每个帐号、每天最多只能查询1000次。目前,Google还没有提出正式的运营模式和收费方式。
Google API目前主要提供三种服务,分别如下:
1)搜索服务。用户提交一个搜索内容请求,Google服务器将查找超过20亿的网页内容,并且将满足用户需求的目标返回给用户,一般来说这种处理只需要几秒的时间。
2)缓存服务(Cache)。用户提交一个URL,Google服务器将返回搜索器访问该URL的最新记录。这样,用户很容易复用Google的搜索成果。
3)拼写检查。该功能用于检查用户的查询请求是否有效。用户提交一个检查内容,Google服务器将返回有效的、最接近该内容的查询请求,返回的查询请求将符合Google的查询规则。

二、相关准备工作
为了开发基于Java的程序,我们需要作如下准备工作。
1)建立Java开发环境,Java SDK 1.3.1 或更新的版本。相关地址http:.//java.sun.com/J2SE
2)下载Google API的Jar文件,Googleapi.jar。开发人员可以到http://www.google.com/apis下载。
3)申请Google访问帐号,目前Google支持免费申请的方式,用户需要使用一个email帐号来获取新帐号。注意,目前免费帐号支持每天1000条的最大查询量。申请地址:http://www.google.com/apis。
4)(可选) 如果用户需要通过Java程序直接发送SOAP请求,开发人员需要下载相关的Java 软件包,JAXM。下载地址:http://java.sun.com/xml

三、SOAP的使用
Google提供了基于SOAP的Web Service,因此用户可以向Google服务器提交SOAP的查询请求,而后Google服务器将处理这些请求,并返回SOAP格式的查询结果。以下就是一个查询请求和查询结果。
查询:例如,有一个查询请求,请求类型为搜索,查询的内容为“world cup”,帐号为“123456789”,如下为该查询的一个例子。
<?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>
查询结果:如果我们执行以上查询,可以得到如下的查询结果。其中,查询结果总共有约2660000记录,使用时间为0.125012秒。Item标记代表一条查询结果,在第一条查询结果中,网站的URL为http://www.fifaworldcup.com。本例子仅仅列举了一条查询结果。

<?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="ns