Using Google Web Services API in Java
| Java Discuss, Using Google Web Services API in Java at Programmers Lounge forum; Google uses SOAP and WSDL standards to allow access from most programming languages. In addition to the SOAP access, Google ... |
| Notices | Welcome to the Gamerz Needs forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |  | 
08-23-2006, 09:43 AM
|  | Armenia | | | Last Online: Today 01:41 PM Join Date: Mar 2006 Location: Boss World :D
Posts: 2,671
Thanks: 225
Thanked 540 Times in 348 Posts
Nominated 0 Times in 0 Posts TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 14 Points: 43,665.01 Bank: 499.61 Total Points: 44,164.62 | | Using Google Web Services API in Java Google uses SOAP and WSDL standards to allow access from most programming languages. In addition to the SOAP access, Google has created a set of Java wrapper classes that encapsulates access to the Google Web APIs. This tutorial discusses the Java classes and the functionality of the Google web services.The Google web services allow you to query Google's web index and to access the Google spell check engine. When performing a search you have access to most features available when searching on Google's web site:- Retrieve a cached web page from Google
- Enable or disable SafeSearch
- Limit the search results by language
Currently, there are limitations on Google's Web API. Some features that aren't currently available through Google's Web API: - Search is limited to the Web search. Currently, there isn't any access to search in the Images, Groups, News, Froogle, or Local categories.
- The search modifies are limited to a subset of the search modifiers that are allowed on google.com (See Related Links). For example, the "site:" modifier is allowed from the web api only if additional search terms are included. Google.com allows for "site:" modifier to be used without any additional search terms to list all pages in a site that have been indexed. In addition, some additional search modifies such as "movie:" to display movie times specified by a zip code.
- Searching in Google's Print index is not currently available.
- Google's calculator, currency conversion, word definition, stock quotes, or weather are not currently available through the Google Web API.
To start using the Google Web API, you will need to download the Google Web APIs developer's kit and register for a Google license key on Google's developer page (see Related Links). Once you have downloaded and unzipped the Google developer's kit, include the googleapi.jar file in your Java class path. You will need to instantiate an instance of the GoogleSearch class and set the key that was provided by Google. GoogleSearch googleSearch = new GoogleSearch(); //Set the key provided by Google googleSearch.setKey("your key"); Next, we need to set the query string that you want to search for in the Google index. If you want to use any search modifiers, include them in the query string. To limit our search results, we set the maximum number of matches to 10. //Search query string googleSearch.setQueryString("population of Japan"); //Limit the search results to 10 googleSearch.setMaxResults(10); For our final steps, we perform the search. After the search is complete, we get the search result elements. The search result elements contain the page title, url, and page snippet.GoogleSearchResult searchResult = googleSearch.doSearch(); GoogleSearchResultElement[] results = searchResult.getResultElements(); On a final note, the search result elements contain HTML formatting to show the hits within the results. In our example, we use a Java's regular expression support to remove the HTML tags. This is a quick and easy way to remove the HTML tags from text. String title = results[index].getTitle(); title = title.replaceAll("<.*?>", ""); The complete Google Web API example is below.package tutorial; import com.google.soap.search.*; public class Google { public Google() { try { GoogleSearch googleSearch = new GoogleSearch(); //Set the clientKey to key provided by Google googleSearch.setKey("your key"); //Search query string googleSearch.setQueryString("population of Japan"); //Limit the search results to 10 googleSearch.setMaxResults(10); GoogleSearchResult searchResult = googleSearch.doSearch(); GoogleSearchResultElement[] results = searchResult.getResultElements(); if (results.length == 0) { System.out.println("No match found."); } else { for (int index = 0; index < results.length; index++) { //Title String title = results[index].getTitle(); title = title.replaceAll("<.*?>", ""); System.out.println(title); //URL String url = results[index].getURL(); System.out.println(url); //Title String summary = results[index].getSnippet(); summary = summary.replaceAll("<.*?>", ""); System.out.println(summary); System.out.println(); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Google googleDemo = new Google(); } } Thank me if you liked it! xD
Credits go to Kevin Long.
__________________ | | The Following User Says Thank You to M0NK3Y For This Useful Post: | |  | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | |