java google custom search api

wolve80 picture wolve80 · Aug 2, 2011 · Viewed 8.4k times · Source

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 started? Thank you!

Answer

subha picture subha · Jun 1, 2012

I want to make a correction here.

customsearch.setKey("YOUR_API_KEY_GOES_HERE");

does not work for client lib 1.6 but following does work

     Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());

    try {
        com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list("YOUR_SEARCH_STRING_GOES_HERE");
        list.setKey("YOUR_API_KEY_GOES_HERE");
        list.setCx("YOUR_CUSTOM_SEARCH_ENGINE_ID_GOES_HERE");
        Search results = list.execute();
        List<Result> items = results.getItems();

        for(Result result:items)
        {
            System.out.println("Title:"+result.getHtmlTitle());

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }