I\'m trying to use the java client for the Google custom search api but couldn\'t find any sample tutorials on the web. Can someone provide a simple example for me to get start
The following example is based on the 1-1.30 client lib. As there isn't much documentation this is definitely not the best example. In fact I'm intentionally using a deprecated method to set the API key as the newer way seemed overly complex.
Assuming you have included the correct jar dependencies in your project's build path, a basic example would be:
//Instantiate a Customsearch object with a transport mechanism and json parser
Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());
//using deprecated setKey method on customsearch to set your API Key
customsearch.setKey("YOUR_API_KEY_GOES_HERE");
//instantiate a Customsearch.Cse.List object with your search string
com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list("YOUR_SEARCH_STRING_GOES_HERE");
//set your custom search engine id
list.setCx("YOUR_CUSTOM_SEARCH_ENGINE_ID_GOES_HERE")
//execute method returns a com.google.api.services.customsearch.model.Search object
Search results = list.execute();
//getItems() is a list of com.google.api.services.customsearch.model.Result objects which have the items you want
List items = results.getItems();
//now go do something with your list of Result objects
You'll need to get a custom search engine id, and an API key from the Google API Console