java google custom search api

后端 未结 4 681
走了就别回头了
走了就别回头了 2021-02-06 12:30

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

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 13:09

    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 items = results.getItems();
    
            for(Result result:items)
            {
                System.out.println("Title:"+result.getHtmlTitle());
    
            }
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

提交回复
热议问题