調用Google API 的Java實現(Web service)

來源:互聯網
上載者:User

實驗內容

掌握調用現有web service的方法。

編寫程式,調用

Web Service的廢話就不說了。就說怎麼使用吧。

1.需要java環境 到http:.//java.sun.com/J2SE  可以下載 Java Development Kit

2.需要Google API的Jar檔案,Googleapi.jar。可以到http://www.google.com/apis下載。填加進CLASSPATH.

3.申請Google訪問帳號,目前Google支援免費申請的方式,使用者需要使用一個email帳號來擷取新帳號。注意,目前免費帳號支援每天1000條的最大查詢量。申請地址:http://www.google.com/apis。

   申請以後去郵箱裡啟用你的帳戶,然後會有另一封email發到郵箱裡。得到你的license key 。

SOAP請求的格式就不說了,google直接提供了api.別浪費了Googleapi.jar哦.

4.以下是例子

package daxigua;

import com.google.soap.search.*;
import java.io.*;

public class GoogleAPITest {
    public static void main(String[] args) {

        String clientKey = "clientKey";//剛才得到的license key ,不是帳戶名稱哦
        String query = "csdn";
        GoogleSearch s = new GoogleSearch();
        s.setKey(clientKey);
        try {
            s.setQueryString(query);
            s.setLanguageRestricts("lang_cn");
            GoogleSearchResult r = s.doSearch();
            System.out.println("Google Search Results:" + r.toString());
            FileWriter fw = new FileWriter("result.txt");
            fw.write(r.toString());
            fw.close();
        } catch (GoogleSearchFault f) {
            System.out.println("The call to the google Web APIs failed:" +
                               f.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

具體的使用參看Googleapi.zip的doc目錄,一共就6個類。 

google發布的web service,使得程式可以發送查詢,並且接受和列印google查詢得到的結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.