SearchService.java
上传用户:shen332233
上传日期:2021-09-03
资源大小:7478k
文件大小:3k
源码类别:

Ajax

开发平台:

Java

  1. package ajaxdashboard.service;
  2. import ajaxdashboard.domain.SearchResult;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import org.jdom.Document;
  12. import org.jdom.Element;
  13. import org.jdom.JDOMException;
  14. import org.jdom.Namespace;
  15. import org.jdom.input.SAXBuilder;
  16. /**
  17.  *
  18.  * @author Owner
  19.  */
  20. public class SearchService {
  21.     private static final String YAHOO_SEARCH_URL = 
  22.         "http://api.search.yahoo.com/WebSearchService/V1/webSearch" 
  23.             + "?appid=thunderboltsoftware"
  24.             + "&type=all"
  25.             + "&results=10"
  26.             + "&query=";
  27.     private InputStream getResultsInputStream(String searchTerm) throws Exception {
  28.         String url = YAHOO_SEARCH_URL + searchTerm;
  29.         System.out.println("url: " + url);
  30.         HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
  31.         con.setDoInput(true);
  32.         con.setDoOutput(true);
  33.         
  34.         con.setRequestMethod("GET");
  35.         
  36.         return con.getInputStream();
  37.     }
  38.     
  39.     public Collection search(String searchTerm) {
  40.         Document resultsXML = getSearchResultsXML(searchTerm);
  41.         return parseResults(resultsXML);
  42.     }
  43.     
  44.     private Document getSearchResultsXML(String searchTerm) {
  45.         SAXBuilder builder = new SAXBuilder();
  46.         Document news = null;
  47.         
  48.         try {
  49.             InputStream searchResultsInputStream = getResultsInputStream(searchTerm);
  50.             news = builder.build(searchResultsInputStream);
  51.         } 
  52.         catch (JDOMException ex) {
  53.             System.out.println("Error: " + ex.toString());
  54.         } 
  55.         catch (IOException ex) {
  56.             System.out.println("Error: " + ex.toString());
  57.         }
  58.         catch(Exception ex) {
  59.             System.out.println("Exception: " + ex.toString());
  60.         }
  61.         
  62.         return news;
  63.         
  64.     }
  65.     
  66.     private Collection parseResults(Document results) {
  67.         Collection searchResults = new ArrayList();
  68.         Namespace ns = Namespace.getNamespace("urn:yahoo:srch");
  69.         
  70.         Element root = results.getRootElement();
  71.         
  72.         List items = root.getChildren("Result", ns);
  73.         System.out.println("res size: " + items.size());
  74.         Element item = null;
  75.         SearchResult result = null;
  76.         for(Iterator it = items.iterator(); it.hasNext();) {
  77.             item = (Element)it.next();
  78.             result = new SearchResult();
  79.             result.setTitle(item.getChildText("Title", ns));
  80.             result.setSummary(item.getChildText("Summary", ns));
  81.             result.setUrl(item.getChildText("Url", ns));
  82.             searchResults.add(result);
  83.         }
  84.         
  85.         return searchResults;
  86.     }
  87.     
  88. }
  89.