posts - 97,  comments - 93,  trackbacks - 0
          昨晚同學找我幫忙寫一個利用GOOGLE API的小程序,也比較感興趣所以就應下了。下載了GOOGLE提供的googleapi.jar,大約花了20多分鐘,寫了這個小程序,由于需要提供GOOGLE key所以也懶得去申請,就沒有測試~~呵呵
          (原來本來一直用netbeans,:),主要是因為做sun的校園大使,現在用eclipse了:),原因也很簡單,是因為要到IBM實習了,不過說實話它們確實都各有各的長處,這里呢用eclipse作的了)。
          目錄結構很簡單   


          類實現主要為:
          *******************
          GSTest.java
          *******************
           1 import com.google.soap.search.*;
           2 
           3 /**
           4  * @author QuQiang
           5  * creat a google search object,and return a set of the search reasult
           6  */
           7 
           8 public class GSTest {
           9 
          10     private String key=ReadPro.getResource("GoogleKeycode");
          11     private String searchContent=ReadPro.getResource("SearchContent");
          12     
          13     GoogleSearch GsTest = new GoogleSearch();
          14     
          15     public GoogleSearchResult SearchResult(){
          16         GsTest.setKey(key);
          17         GoogleSearchResult GSR = new GoogleSearchResult();
          18         try {
          19             GsTest.setQueryString(searchContent);
          20             GSR = GsTest.doSearch();
          21         } catch (GoogleSearchFault e) {
          22             e.printStackTrace();
          23         }
          24         return GSR;
          25     }
          26 }
          *******************
          ReadPro.java
          *******************
           1 package sun.org.nicky.myorgtest;
           2 
           3 /**
           4  * @author QuQiang
           5  *
           6  */
           7     import java.io.InputStream;
           8     import java.io.IOException;
           9     import java.util.Properties;
          10 
          11     public class ReadPro{
          12        private static Properties property = null;
          13       
          14        static{
          15           InputStream stream = null;
          16           try{
          17             stream=ReadPro.class.getResourceAsStream("/resource/app.property");
          18             property = new Properties();
          19             property.load(stream);
          20           }catch(IOException e){
          21               e.printStackTrace();
          22           }finally{
          23               if(stream != null){
          24                  try{
          25                     stream.close();
          26                  }catch(IOException e){
          27                     e.printStackTrace();
          28                  }           
          29               }
          30           }
          31        }
          32        public static String getResource(String key){
          33          if(property == null){
          34            return null;// init error;
          35          }
          36          
          37          return property.getProperty(key);
          38        }
          39     }
          *******************
          Main.java
          *******************
           1 package sun.org.nicky.myorgtest;
           2 
           3 import java.io.File;
           4 import java.io.FileNotFoundException;
           5 import java.io.FileOutputStream;
           6 import java.io.PrintStream;
           7 
           8 import com.google.soap.search.GoogleSearchResult;
           9 import com.google.soap.search.GoogleSearchResultElement;
          10 
          11 /**
          12  * @author QuQiang Main function
          13  */
          14 public class Main {
          15     private static File file = new File("search.html");
          16 
          17     public static void main(String[] args) {
          18         GSTest newOGST = new GSTest();
          19         GoogleSearchResult newOGSTR = newOGST.SearchResult();
          20         GoogleSearchResultElement[] GSTRArray = new GoogleSearchResultElement[newOGSTR
          21                 .getResultElements().length];
          22         FileOutputStream fs = null;
          23         PrintStream p = null;
          24         try {
          25             fs = new FileOutputStream(file);
          26             p = new PrintStream(fs);
          27             p.append("<!--Result Returned by Google Search Engine-->\n");
          28             for (int i = 0; i < GSTRArray.length; i++) {
          29                 p.append(GSTRArray[i].toString());
          30                 System.out.println(GSTRArray[i].toString());
          31             }
          32         } catch (FileNotFoundException e) {
          33             e.printStackTrace();
          34         } finally {
          35             p.close();
          36         }
          37     }
          38 }
          至于 app.property就更簡單了,不過key需要自己填
          1 GoogleKeycode        = ******
          2 SearchContent        = ******



          posted on 2007-04-14 15:22 wqwqwqwqwq 閱讀(3567) 評論(9)  編輯  收藏 所屬分類: Simple Java

          FeedBack:
          # re: 利用google api寫的小程序 
          2007-11-04 18:45 | 風中之歌
          google里找不到那個googleapi.jar,能傳我一個嗎?
          sakura-2008@163.com
          萬分感謝!  回復  更多評論
            
          # re: 利用google api寫的小程序 
          2008-01-10 22:54 | wangli
          您好,也能否傳我一個啊?rcwangli@126.com謝謝了。  回復  更多評論
            
          # re: 利用google api寫的小程序 
          2008-01-12 21:30 | nicky
          都已經發給你們了
          現在據說google已經不再提供那個的key的申請了,我想有四個考慮的方式
          第一,這并不說明,原來別人申請的不會奏效。
          第二,大家也可以使用其他的比如baidu yahoo的搜索結果。
          第三,你可以使用非ws的方式獲取,比如socket,但是要注意連接的ip,時間次數,服務器也是很聰明的^^。
          第四,按步就搬咯,去google code lab之類的去研究下它現在提供給自由開發者的解決方案。
          一己之見,有待補充。
            回復  更多評論
            
          # re: 利用google api寫的小程序 [未登錄]
          2008-01-14 11:00 | libin
          我要用這個搜索result的數目做實驗,
          但是現在新的版本好像不提供這個接口了
          老版本的提供嗎?
          請指教
          我的郵箱是wbreadlee@gmail.com麻煩發給我好嗎? 還有那個key  回復  更多評論
            
          # re: 利用google api寫的小程序 
          2008-01-21 13:03 | rcwangli
          我幾天沒打開郵箱了,覺得沒有人會理會我,今天一打開竟然收到了,太謝謝了,贊一個^^  回復  更多評論
            
          # re: 利用google api寫的小程序 
          2008-01-23 15:59 | monke82
          麻煩也發一個給我吧,謝謝了阿。
          monke82@163.com  回復  更多評論
            
          # re: 利用google api寫的小程序 
          2008-01-29 01:48 | kefon
          可不可以也給我一個?kefon@163.com 謝謝  回復  更多評論
            
          # re: 利用google api寫的小程序 
          2008-03-18 16:08 | dustin.xu
          如果可以,能否也給我一個key,畢設需要,搜了group兩天也沒找到,就快崩潰了
          dustin.xu@gmail.com  回復  更多評論
            
          # Seo News
          2009-05-18 20:53 | Seo News
          Give please. Inspiration does exist, but it must find you working.
          I am from Zambia and , too, and now am writing in English, give true I wrote the following sentence: "Discover performance based seo services.Left, bullet seo optimization, right."

          THX :-D, Wilford.  回復  更多評論
            
          <2009年5月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456




          常用鏈接

          留言簿(10)

          隨筆分類(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊

          J2ME技術網站

          java技術相關

          mess

          搜索

          •  

          最新評論

          閱讀排行榜

          校園夢網網絡電話,中國最優秀的網絡電話
          主站蜘蛛池模板: 安溪县| 金昌市| 孟津县| 保靖县| 凤城市| 景洪市| 六安市| 安宁市| 健康| 衡阳县| 汉中市| 古浪县| 荃湾区| 财经| 新竹县| 曲水县| 深圳市| 进贤县| 丽水市| 桦南县| 乌拉特前旗| 郎溪县| 克什克腾旗| 兴化市| 金秀| 山东省| 察哈| 金寨县| 纳雍县| 根河市| 泗水县| 社会| 桦川县| 惠东县| 长武县| 大厂| 斗六市| 卓尼县| 浪卡子县| 阿克陶县| 姚安县|