用google web API编程的步骤:
1)下载开发工具包
这个工具包中提供了文档和调用Google web API服务的例程,例程有java和.net两类,以及一个WSDL文件以供在任何支持web服务的平台上编程之用。
2)创建一个google帐号
为了访问google Web API服务,你必须创建一个google帐号并取得授权码。帐号和授权码可使你一天内用程序查询1000次。
我们要写一个javabean来调用google的API
代码如下:
package googleservlet;
import com.google.soap.search.*;
public class GoogleBean {
private GoogleSearch search;
private GoogleSearchResult googleRes;
private final static String GOOGLE_KEY = "enxr3XRQertgf5l5ow/GyIE9TIwJQT/Uu";
// 这个key是你重google申请的
private String lineSep = "/n";
//Settable bean properties
private String query; //存放查询的关键字
private boolean filter;
private int maxResults;
private int startRes;
private boolean safeSearch;
private String restrict;
private String langRestrict;
public GoogleBean(){
query = "";
restrict = "";
langRestrict = "";
}
public String structureResult(GoogleSearchResult res){
GoogleSearchResultElement[] elements = res.getResultElements(); //获取查询的结果,存放在数组里
String url ="";
String results = "Estimated total results count: " +
res.getEstimatedTotalResultsCount() + lineSep + lineSep;
System.out.println(elements.length);
for (int i = 0; i < elements.length; i++){ //提取查询的结果的URL
url = elements[i].getURL();
results += ("Title: " + elements[i].getTitle() + lineSep +
"URL: <a href="/blog//"";" + url + "/">" + url + "</a>"+ lineSep +
"Summary: " + elements[i].getSummary() + lineSep +
"Snippet: " + elements[i].getSnippet() + lineSep + lineSep);
}
return results;
}
public String getSearchResults() throws GoogleSearchFault {
search = new GoogleSearch();
search.setKey(GOOGLE_KEY); //设置你的google key
search.setFilter(filter);
if(restrict.length() > 0)
search.setRestrict(restrict);
search.setQueryString(query); //设置查询
googleRes = search.doSearch(); //开始搜索
return structureResult(googleRes);
}
public void setLineSep(String lineSep){
this.lineSep=lineSep;
}
public String getLineSep(){
return lineSep;
}
public void setQuery(String query){
this.query = query;
}
public String getQuery(){
return query;
}
public void setRestrict(String query){
this.restrict = restrict;
}
public String getRestrict(){
return restrict;
}
public void setLangRestrict(String query){
this.langRestrict = langRestrict;
}
public String getLangRestrict(){
return langRestrict;
}
public void setFilter(boolean filter){
this.filter = filter;
}
public boolean getFilter(){
return filter;
}
public void setSafeSearch(boolean filter){
this.safeSearch = safeSearch;
}
public boolean getSafeSearch(){
return safeSearch;
}
public void setMaxResults(int maxResults){
this.maxResults = maxResults;
}
public int getMaxResults(){
return maxResults;
}
public void setStartRes(int startRes){
this.startRes = startRes;
}
public int getStartRes(){
return startRes;
}
public void release(){
search = null;
googleRes = null;
}
}
这个servlet为主查询的界面和调用javabean
package googleservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class GoogleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
//set the MIME type of the response, "text/html"
response.setContentType("text/html");
//use a PrintWriter send text data to the client who has requested the servlet
java.io.PrintWriter out = response.getWriter();
//Begin assembling the HTML content
out.println("<html><head>");
out.println("<title>Initiate a Google Search</title></head><body>");
out.println("<h2>Please enter your search terms</h2>");
out.println(
"<form method=/"POST/" action =/"" + request.getContextPath() +
"/googleservlet/" target=/"window_name/" >");
out.println("<table border=/"0/"><tr><td valign=/"top/">");
out.println("Search terms: </td> <td valign=/"top/">");
out.println("<input type=/"text/" name=/"query/" size=/"15/">");
out.println("</td></tr><tr><td valign=/"top/">");
out.println("Restrict to Google sub-site... </td> <td valign=/"top/">");
out.println("<select name=/"restrict/"><option selected>none</option><option>unclesam</option><option>linux</option><option>mac</option><option>bsd</option></select>");
out.println("</td></tr><tr><td valign=/"top/">");
out.println("<input type=/"submit/" value=/"Submit Info/"></td></tr>");
out.println("</table></form>");
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,java.io.IOException{
String query = request.getParameter("query");
String restrict = request.getParameter("restrict");
boolean isValid = (query == null || query.length() < 1) ? false : true;
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Google results</title></head><body>");
if (! isValid){
out.println("<h2>Sorry, the query parameter was either empty or null</h2>");
} else {
out.println("<h2>Here are your search results</h2>");
GoogleBean gb = new GoogleBean();
gb.setFilter(true);
gb.setLineSep("<br /><br />");
if (restrict != null && restrict.length() > 0)
gb.setRestrict(restrict);
gb.setQuery(query);
try {
out.println( gb.getSearchResults() );
} catch (Exception e){
throw new ServletException( e.getMessage() );
}
}
out.println("</body></html>");
out.close();
}
}