精彩的人生

          好好工作,好好生活

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks
          一、簡介
          Google搜索引擎提供了基于SOAP的Web Service。這意味著不同的開發(fā)語言、開發(fā)環(huán)境都能夠使用這種服務,另外,Google為了簡化Java程序員的開發(fā),它還提供了一套Java API接口,用于訪問Web Serivce,這使得開發(fā)一套支持Google搜索功能的程序變得十分容易。開發(fā)人員可以將Google的搜索功能嵌入到他們的應用程序當中。本文將介紹如何使用這些Java API,以及如何使用google的Web Service。
          目前,Google的API還是處于Beta測試階段,因此,在API的使用上還有一些限制。例如,使用這些API需要申請一個帳號。對于免費申請的帳號,為了防止開發(fā)人員不正當?shù)氖褂?,限制每個帳號、每天最多只能查詢1000次。目前,Google還沒有提出正式的運營模式和收費方式。
          Google API目前主要提供三種服務,分別如下:
          1)搜索服務。用戶提交一個搜索內(nèi)容請求,Google服務器將查找超過20億的網(wǎng)頁內(nèi)容,并且將滿足用戶需求的目標返回給用戶,一般來說這種處理只需要幾秒的時間。
          2)緩存服務(Cache)。用戶提交一個URL,Google服務器將返回搜索器訪問該URL的最新記錄。這樣,用戶很容易復用Google的搜索成果。
          3)拼寫檢查。該功能用于檢查用戶的查詢請求是否有效。用戶提交一個檢查內(nèi)容,Google服務器將返回有效的、最接近該內(nèi)容的查詢請求,返回的查詢請求將符合Google的查詢規(guī)則。

          二、相關(guān)準備工作
          為了開發(fā)基于Java的程序,我們需要作如下準備工作。
          1)建立Java開發(fā)環(huán)境,Java SDK 1.3.1 或更新的版本。相關(guān)地址http:.//java.sun.com/J2SE
          2)下載Google API的Jar文件,Googleapi.jar。開發(fā)人員可以到http://www.google.com/apis 下載。
          3)申請Google訪問帳號,目前Google支持免費申請的方式,用戶需要使用一個email帳號來獲取新帳號。注意,目前免費帳號支持每天1000條的最大查詢量。申請地址:http://www.google.com/apis 。
          4)(可選) 如果用戶需要通過Java程序直接發(fā)送SOAP請求,開發(fā)人員需要下載相關(guān)的Java 軟件包,JAXM。下載地址:http://java.sun.com/xml

          三、SOAP的使用
          Google提供了基于SOAP的Web Service,因此用戶可以向Google服務器提交SOAP的查詢請求,而后Google服務器將處理這些請求,并返回SOAP格式的查詢結(jié)果。以下就是一個查詢請求和查詢結(jié)果。
          查詢:例如,有一個查詢請求,請求類型為搜索,查詢的內(nèi)容為“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>
          查詢結(jié)果:如果我們執(zhí)行以上查詢,可以得到如下的查詢結(jié)果。其中,查詢結(jié)果總共有約2660000記錄,使用時間為0.125012秒。Item標記代表一條查詢結(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為了提高開發(fā)人員的開發(fā)效率,提供了一套基于Java的API,用于直接訪問Google服務器。該API包裝了Google的Web Service,使用起來比Web Service更加方便。該API 包主要包括如下幾個類:
          GoogleSearch :該類封裝了對于Google服務器的訪問,支持搜索和緩存(cache)功能。
          GoogleSearchDirectoryCategory :表示Google中的一個分類目錄
          GoogleSearchFault :該類為Exception的一個子類,用于表示API使用的錯誤。
          GoogleSearchResult :該類封裝了搜索的結(jié)果。
          GoogleSearchResultElement:該類表示搜索結(jié)果中的每一條記錄。

          如下是一個例子的源代碼,用于一個簡單查詢,查詢請求為”world Cup”,查詢的帳號為”123456789”。該例子將把查詢結(jié)果打印出來。如果用戶需要近一步解析查詢結(jié)果,可以使用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());
          }
          }
          }

          五、總結(jié)
          Google搜索引擎提供了搜索、緩存和拼寫簡單功能,通過Web Service可以在不同的平臺、不同的語言使用,為了簡化SOAP的開發(fā),Google還提供了包裝了SOAP服務的Java API,這也大大提高了Java程序員的開發(fā)效率。隨著搜索內(nèi)容、形式的豐富,更多的開發(fā)人員可以將這種技術(shù)運用在自己應用程序中,擴展應用程度的功能。
          posted on 2006-03-15 13:35 hopeshared 閱讀(1659) 評論(0)  編輯  收藏 所屬分類: JavaWeb Service
          主站蜘蛛池模板: 邵阳县| 酒泉市| 于都县| 无为县| 抚远县| 镇巴县| 甘孜| 疏勒县| 玛沁县| 运城市| 景宁| 上蔡县| 葫芦岛市| 江都市| 乐都县| 德昌县| 景德镇市| 巨鹿县| 沅江市| 海门市| 秀山| 吴江市| 小金县| 广饶县| 洛南县| 济源市| 河津市| 灌南县| 商水县| 宁乡县| 苏尼特左旗| 定边县| 赞皇县| 新沂市| 西平县| 浦县| 确山县| 沾益县| 万宁市| 儋州市| 扬中市|