The following example is based on 1-1.30 client lib . Since there is not much documentation, this is definitely not the best example. In fact, I intentionally use an outdated method to set the API key, since the newer method seemed too complicated.
Assuming you have included the correct jar dependencies in the project build path, a basic example might 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<Result> items = results.getItems(); //now go do something with your list of Result objects
You will need to get the user search engine identifier and API key from the Google API Console
source share