使用谷歌自定义搜索API的Java代码

任何人都可以分享一些java代码开始使用谷歌搜索API。我在互联网上搜索但没有找到任何适当的文档或良好的示例代码。我找到的代码似乎没有工作。如果有人,我将感激不尽可以帮助我。(我已经获得了API密钥和自定义搜索引擎ID)。

谢谢。

我在上面的@Zakaria提供的代码中更改了while loop 。 它可能不是一种正确的解决方法,但它会为您提供谷歌搜索的结果链接。 您只需要解析输出。 看这里,

 public static void main(String[] args) throws Exception { String key="YOUR KEY"; String qry="Android"; URL url = new URL( "https://www.googleapis.com/customsearch/v1?key="+key+ "&cx=013036536707430787589:_pqjad5hr1a&q="+ qry + "&alt=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { if(output.contains("\"link\": \"")){ String link=output.substring(output.indexOf("\"link\": \"")+("\"link\": \"").length(), output.indexOf("\",")); System.out.println(link); //Will print the google search links } } conn.disconnect(); } 

希望它也适合你。

好吧,我认为你可以使用Java RESTFUL客户端没有什么特别之处。

我使用该Java代码并基于Google文档尝试了Custom API:

 public static void main(String[] args) throws IOException { URL url = new URL( "https://www.googleapis.com/customsearch/v1?key=YOUR-KEY&cx=013036536707430787589:_pqjad5hr1a&q=flowers&alt=json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } 

您必须将“YOUR-KEY”替换为Google API控制台上的“YOUR-KEY”。

对于需要使用Google库的自定义搜索API工作示例的任何人,您可以使用以下方法:

 public static List search(String keyword){ Customsearch customsearch= null; try { customsearch = new Customsearch(new NetHttpTransport(),new JacksonFactory(), new HttpRequestInitializer() { public void initialize(HttpRequest httpRequest) { try { // set connect and read timeouts httpRequest.setConnectTimeout(HTTP_REQUEST_TIMEOUT); httpRequest.setReadTimeout(HTTP_REQUEST_TIMEOUT); } catch (Exception ex) { ex.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } List resultList=null; try { Customsearch.Cse.List list=customsearch.cse().list(keyword); list.setKey(GOOGLE_API_KEY); list.setCx(SEARCH_ENGINE_ID); Search results=list.execute(); resultList=results.getItems(); } catch ( Exception e) { e.printStackTrace(); } return resultList; } 

此方法返回结果列表对象,因此您可以遍历它

  List results = new ArrayList<>(); try { results = search(QUERY); } catch (Exception e) { e.printStackTrace(); } for(Result result : results){ System.out.println(result.getDisplayLink()); System.out.println(result.getTitle()); // all attributes: System.out.println(result.toString()); } 

您已经注意到,您必须定义自定义GOOGLE_API_KEY,SEARCH_ENGINE_ID,QUERY和HTTP_REQUEST_TIMEOUT,即

 private static final int HTTP_REQUEST_TIMEOUT = 3 * 600000; 

我使用gradle依赖:

 dependencies { compile 'com.google.apis:google-api-services-customsearch:v1-rev57-1.23.0' }