昨晚同学找我帮忙写一个利用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 阅读(3553)
评论(9) 编辑 收藏 所属分类:
Simple Java