<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    精彩的人生

    好好工作,好好生活

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks
    一、簡介
    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="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為了提高開發人員的開發效率,提供了一套基于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程序員的開發效率。隨著搜索內容、形式的豐富,更多的開發人員可以將這種技術運用在自己應用程序中,擴展應用程度的功能。
    posted on 2006-03-15 13:35 hopeshared 閱讀(1657) 評論(0)  編輯  收藏 所屬分類: Java 、Web Service
    主站蜘蛛池模板: 好爽…又高潮了毛片免费看 | 九九久久国产精品免费热6| 亚洲欧洲精品成人久久曰影片 | 亚洲一卡2卡3卡4卡乱码 在线 | 亚洲愉拍99热成人精品热久久 | 久久久久久国产精品免费无码| 伊人久久亚洲综合影院首页| 亚洲欧洲精品成人久久奇米网| 98精品全国免费观看视频| 亚洲av日韩综合一区久热| 久久精品国产精品亚洲蜜月| 在线免费观看中文字幕| 亚洲免费观看视频| 亚洲精品理论电影在线观看| 国产亚洲婷婷香蕉久久精品| 免费观看一级毛片| 蜜桃视频在线观看免费视频网站WWW| 亚洲最大的成人网| 久久精品国产亚洲av麻| 国产成人无码a区在线观看视频免费| 一级特黄aa毛片免费观看| 黄色网页免费观看| 亚洲精品天堂在线观看| 亚洲精品中文字幕乱码三区| 免费观看的毛片手机视频| 久久青草免费91线频观看站街| 青青免费在线视频| 亚洲日韩看片无码电影| 久久亚洲私人国产精品vA| 亚洲性在线看高清h片| 在线不卡免费视频| 57PAO成人国产永久免费视频| 中文字幕在线视频免费观看| 国产亚洲综合视频| 中文字幕亚洲男人的天堂网络| 久久久久久亚洲av成人无码国产| 亚洲成a人无码av波多野按摩| 成人毛片免费网站| 无遮免费网站在线入口| 99re6免费视频| 免费在线观影网站|