使用Google的Web Service
2024-07-21 02:21:20
供稿:網(wǎng)友
 
一、簡(jiǎn)介 
google搜索引擎提供了基于soap的web service。這意味著不同的開(kāi)發(fā)語(yǔ)言、開(kāi)發(fā)環(huán)境都能夠使用這種服務(wù),另外,google為了簡(jiǎn)化java程序員的開(kāi)發(fā),它還提供了一套java api接口,用于訪問(wèn)web serivce,這使得開(kāi)發(fā)一套支持google搜索功能的程序變得十分容易。開(kāi)發(fā)人員可以將google的搜索功能嵌入到他們的應(yīng)用程序當(dāng)中。本文將介紹如何使用這些java api,以及如何使用google的web service。 
目前,google的api還是處于beta測(cè)試階段,因此,在api的使用上還有一些限制。例如,使用這些api需要申請(qǐng)一個(gè)帳號(hào)。對(duì)于免費(fèi)申請(qǐng)的帳號(hào),為了防止開(kāi)發(fā)人員不正當(dāng)?shù)氖褂茫拗泼總€(gè)帳號(hào)、每天最多只能查詢1000次。目前,google還沒(méi)有提出正式的運(yùn)營(yíng)模式和收費(fèi)方式。 
google api目前主要提供三種服務(wù),分別如下: 
1)搜索服務(wù)。用戶提交一個(gè)搜索內(nèi)容請(qǐng)求,google服務(wù)器將查找超過(guò)20億的網(wǎng)頁(yè)內(nèi)容,并且將滿足用戶需求的目標(biāo)返回給用戶,一般來(lái)說(shuō)這種處理只需要幾秒的時(shí)間。 
2)緩存服務(wù)(cache)。用戶提交一個(gè)url,google服務(wù)器將返回搜索器訪問(wèn)該url的最新記錄。這樣,用戶很容易復(fù)用google的搜索成果。 
3)拼寫(xiě)檢查。該功能用于檢查用戶的查詢請(qǐng)求是否有效。用戶提交一個(gè)檢查內(nèi)容,google服務(wù)器將返回有效的、最接近該內(nèi)容的查詢請(qǐng)求,返回的查詢請(qǐng)求將符合google的查詢規(guī)則。 
二、相關(guān)準(zhǔn)備工作 
為了開(kāi)發(fā)基于java的程序,我們需要作如下準(zhǔn)備工作。 
1)建立java開(kāi)發(fā)環(huán)境,java sdk 1.3.1 或更新的版本。相關(guān)地址http:.//java.sun.com/j2se 
2)下載google api的jar文件,googleapi.jar。開(kāi)發(fā)人員可以到http://www.google.com/apis下載。 
3)申請(qǐng)google訪問(wèn)帳號(hào),目前google支持免費(fèi)申請(qǐng)的方式,用戶需要使用一個(gè)email帳號(hào)來(lái)獲取新帳號(hào)。注意,目前免費(fèi)帳號(hào)支持每天1000條的最大查詢量。申請(qǐng)地址:http://www.google.com/apis。 
4)(可選) 如果用戶需要通過(guò)java程序直接發(fā)送soap請(qǐng)求,開(kāi)發(fā)人員需要下載相關(guān)的java 軟件包,jaxm。下載地址:http://java.sun.com/xml 
三、soap的使用 
google提供了基于soap的web service,因此用戶可以向google服務(wù)器提交soap的查詢請(qǐng)求,而后google服務(wù)器將處理這些請(qǐng)求,并返回soap格式的查詢結(jié)果。以下就是一個(gè)查詢請(qǐng)求和查詢結(jié)果。 
查詢:例如,有一個(gè)查詢請(qǐng)求,請(qǐng)求類(lèi)型為搜索,查詢的內(nèi)容為“world cup”,帳號(hào)為“123456789”,如下為該查詢的一個(gè)例子。 
<?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> 
查詢結(jié)果:如果我們執(zhí)行以上查詢,可以得到如下的查詢結(jié)果。其中,查詢結(jié)果總共有約2660000記錄,使用時(shí)間為0.125012秒。item標(biāo)記代表一條查詢結(jié)果,在第一條查詢結(jié)果中,網(wǎng)站的url為http://www.fifaworldcup.com。本例子僅僅列舉了一條查詢結(jié)果。 
<?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> 
<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>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> 
四、使用google api 
google為了提高開(kāi)發(fā)人員的開(kāi)發(fā)效率,提供了一套基于java的api,用于直接訪問(wèn)google服務(wù)器。該api包裝了google的web service,使用起來(lái)比web service更加方便。該api 包主要包括如下幾個(gè)類(lèi): 
googlesearch :該類(lèi)封裝了對(duì)于google服務(wù)器的訪問(wèn),支持搜索和緩存(cache)功能。 
googlesearchdirectorycategory :表示google中的一個(gè)分類(lèi)目錄 
googlesearchfault :該類(lèi)為exception的一個(gè)子類(lèi),用于表示api使用的錯(cuò)誤。 
googlesearchresult :該類(lèi)封裝了搜索的結(jié)果。 
googlesearchresultelement:該類(lèi)表示搜索結(jié)果中的每一條記錄。 
如下是一個(gè)例子的源代碼,用于一個(gè)簡(jiǎn)單查詢,查詢請(qǐng)求為”world cup”,查詢的帳號(hào)為”123456789”。該例子將把查詢結(jié)果打印出來(lái)。如果用戶需要近一步解析查詢結(jié)果,可以使用googlesearchresult和googlesearchresultelement類(lèi)。 
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()); 
} 
} 
} 
五、總結(jié) 
google搜索引擎提供了搜索、緩存和拼寫(xiě)簡(jiǎn)單功能,通過(guò)web service可以在不同的平臺(tái)、不同的語(yǔ)言使用,為了簡(jiǎn)化soap的開(kāi)發(fā),google還提供了包裝了soap服務(wù)的java api,這也大大提高了java程序員的開(kāi)發(fā)效率。隨著搜索內(nèi)容、形式的豐富,更多的開(kāi)發(fā)人員可以將這種技術(shù)運(yùn)用在自己應(yīng)用程序中,擴(kuò)展應(yīng)用程度的功能。