二、相關準備工作
為了開發基于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="‘ 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>
查詢結果:如果我們執行以上查詢,可以得到如下的查詢結果。其中,查詢結果總共有約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="‘ 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為了提高開發人員的開發效率,提供了一套基于Java的API,用于直接訪問Google服務器。該API包裝了Google的Web Service,使用起來比Web Service更加方便。該API 包主要包括如下幾個類:
GoogleSearch :該類封裝了對于Google服務器的訪問,支持搜索和緩存(cache)功能。
GoogleSearchDirectoryCategory :表示Google中的一個分類目錄
GoogleSearchFault :該類為Exception的一個子類,用于表示API使用的錯誤。
GoogleSearchResult :該類封裝了搜索的結果。
GoogleSearchResultElement:該類表示搜索結果中的每一條記錄。
如下是一個例子的源代碼,用于一個簡單查詢,查詢請求為”world Cup”,查詢的賬號為”123456789”。該例子將把查詢結果打印出來。如果用戶需要近一步解析查詢結果,可以使用GoogleSearchResult和GoogleSearchResultElement類。
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());
}
}
}
五、總結
Google搜索引擎提供了搜索、緩存和拼寫簡單功能,通過Web Service可以在不同的平臺、不同的語言使用,為了簡化SOAP的開發,Google還提供了包裝了SOAP服務的Java API,這也大大提高了Java程序員的開發效率。隨著搜索內容、形式的豐富,更多的開發人員可以將這種技術運用在自己應用程序中,擴展應用程度的功能。