二、相關(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訪(fǎng)問(wèn)賬號(hào),目前Google支持免費(fèi)申請(qǐng)的方式,用戶(hù)需要使用一個(gè)email賬號(hào)來(lái)獲取新賬號(hào)。注意,目前免費(fèi)賬號(hào)支持每天1000條的最大查詢(xún)量。申請(qǐng)地址:http://www.google.com/apis。
4)(可選) 如果用戶(hù)需要通過(guò)Java程序直接發(fā)送SOAP請(qǐng)求,開(kāi)發(fā)人員需要下載相關(guān)的Java 軟件包,JAXM。下載地址:http://java.sun.com/xml
三、SOAP的使用
Google提供了基于SOAP的Web Service,因此用戶(hù)可以向Google服務(wù)器提交SOAP的查詢(xún)請(qǐng)求,而后Google服務(wù)器將處理這些請(qǐng)求,并返回SOAP格式的查詢(xún)結(jié)果。以下就是一個(gè)查詢(xún)請(qǐng)求和查詢(xún)結(jié)果。
查詢(xún):例如,有一個(gè)查詢(xún)請(qǐng)求,請(qǐng)求類(lèi)型為搜索,查詢(xún)的內(nèi)容為“world cup”,賬號(hào)為“123456789”,如下為該查詢(xún)的一個(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="‘ target=_blank>http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1: doGoogleSearch xmlns:ns1="urn:GoogleSearch"
SOAP-ENV:encodingStyle=
"‘ target=_blank>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>
查詢(xún)結(jié)果:如果我們執(zhí)行以上查詢(xún),可以得到如下的查詢(xún)結(jié)果。其中,查詢(xún)結(jié)果總共有約2660000記錄,使用時(shí)間為0.125012秒。Item標(biāo)記代表一條查詢(xún)結(jié)果,在第一條查詢(xún)結(jié)果中,網(wǎng)站的URL為http://www.fifaworldcup.com。本例子僅僅列舉了一條查詢(xún)結(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="‘ target=_blank>http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:doGoogleSearchResponse
xmlns:ns1="urn:GoogleSearch"
SOAP-ENV:encodingStyle="‘ target=_blank>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">
</searchQuery>
</return>
</ns1:doGoogleSearchResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
四、使用Google API
Google為了提高開(kāi)發(fā)人員的開(kāi)發(fā)效率,提供了一套基于Java的API,用于直接訪(fǎng)問(wèn)Google服務(wù)器。該API包裝了Google的Web Service,使用起來(lái)比Web Service更加方便。該API 包主要包括如下幾個(gè)類(lèi):
GoogleSearch :該類(lèi)封裝了對(duì)于Google服務(wù)器的訪(fǎng)問(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)單查詢(xún),查詢(xún)請(qǐng)求為”world Cup”,查詢(xún)的賬號(hào)為”123456789”。該例子將把查詢(xún)結(jié)果打印出來(lái)。如果用戶(hù)需要近一步解析查詢(xún)結(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)用程度的功能。
聯(lián)系客服