Creating a JavaBean to Connect with Google API
By: Bruce W. Perry in Java Beans Tutorials on 2008-11-24
The first thing to do is get set up with a Google Web Services account. Now create a JavaBean that will make keyword searches of Google and return the results.
Example java bean below, first imports the package contained in googleapi.jar: com.google.soap.search. Remember, you stored that JAR file in WEB-INF/lib. This means that the web application can find the Java classes in that package and the GoogleBean in the below sample program can use it.
A JavaBean that searches Google's web database
import com.google.soap.search.*; public class GoogleBean { private GoogleSearch search; private GoogleSearchResult googleRes; private final static String GOOGLE_KEY = "5W1BWPyzPSyI3rIa5Pt3DtXMatsniSGB"; 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(){ //No-arguments constructor for the bean query = ""; restrict = ""; langRestrict = ""; } public String structureResult(GoogleSearchResult res){ //Each GoogleSearchResultElement GoogleSearchResultElement[] elements = res.getResultElements(); String url =""; String results = "Estimated total results count: " + res.getEstimatedTotalResultsCount() + lineSep + lineSep; for (int i = 0; i < elements.length; i++){ url = elements[i].getURL(); results += ("Title: " + elements[i].getTitle() + lineSep + "URL: <a href=\"" + 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); 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 langRestrict){ 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 safeSearch){ 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; } }//GoogleBean
The interesting action in the program above occurs in the methods getSearchResults() and structureResults().
In getSearchResults(), the code creates a GoogleSearch object, which is then customized with Google search options before the GoogleSearch doSearch()method is called. The GoogleSearch object uses setter methods to design a specific google.com search. For example, the setQueryString() method provides the user's search terms. The Java objects that are using the bean provides the search terms by calling the bean's setQuery() method.
You can set the various options for Google searches by calling the GoogleSearch setter methods. For example, calling setFilter(true) filters out all the results that derive from the same web host. And you can restrict the search to specific Google subsites by calling setRestrict("mac"). See http://www.google.com/apis/reference.html.
Every SOAP-related search of Google must call the GoogleSearch setKey() method with the proper license key, or the search is rejected.
The structureResults() method formats the search results. Google search results are encapsulated by a GoogleSearchResult object. This object contains an array of GoogleSearchResultElement objects, which represent each URL that the Google search has returned. The GoogleSearchResult getResultElements() method returns the GoogleSearchResultElement array.
The code then iterates through the array. Each returned element (the GoogleSearchResultElement object) has getter or accessor methods that provides information about the web-page search result:
-
getURL() returns the URL of the found item
-
getTitle() returns the title of the found HTML page
-
getSnippet() returns a snippet (a small, possibly ambiguous, piece of text from the web page)
-
getSummary() returns a text summary of the found web page
The bean uses these methods to display the URL, title, snippet, and summary of each web page the search returns.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java Beans ) |
Latest Articles (in Java Beans) |
Comments