2012-04-21 55 views
10

任何人都可以请分享一些java代码入门谷歌搜索api's.I在互联网上搜索,但没有找到任何适当的文档或良好的示例代码。我发现的代码似乎并不(我已经获得了API密钥和自定义搜索引擎ID)。如果有人能帮助我,我会很感激。使用谷歌自定义搜索API的Java代码

谢谢。

+0

可能重复http://stackoverflow.com/q/3727662/776084 – RanRag 2012-04-21 08:14:16

+1

@RanRag:我硝基甲苯认为这是因为在这里重复我知道谷歌定制搜索API.The我唯一要求的是一些很好的java代码来开始使用它。 – 2012-04-21 08:22:39

回答

11

我在上面的@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();        
} 

希望它也适用于你。

+2

我收到链接的详细信息,但是你知道有什么方法可以获取从这些链接打开的网页详细信息。 – Aniket 2013-03-30 13:14:25

+0

这给了我一个HTTP错误代码400.是否有限制每天可以进行多少查询?如果是这样,那么这个限制是多少? – 2014-10-17 07:15:18

+0

@Pargat我得到'java.io.IOException:服务器返回的HTTP响应代码:403'帮我解决这个问题,我只用上面的代码 – Selva 2015-08-20 11:50:32

5

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

我尝试使用that Java code和基础上的Google documentation自定义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(); 
    } 

你有一对Google APIs Console发现,以取代 “YOUR-KEY”。

+0

我运行的代码,但输出不符合我的期望。我想我会从代码中得到一些顶级链接。你能告诉我如何从这段代码实现? – 2012-04-21 10:35:30

+1

@Zakaria我得到的链接的详细信息,但你知道任何方式来获取从这些链接打开的网页细节。 – Aniket 2013-03-30 13:14:46

2

对于谁可以使用谷歌图书馆需要自定义搜索API的工作例如,你可以使用这个方法:

public static List<Result> 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<Result> 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<Result> 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,查询和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' 
} 
+0

感谢您的回答!我现在正在处理这个问题,并发现你的代码很有用。我喜欢你如何设置API密钥和搜索引擎ID,而不是像其他答案中那样使用URL字符串。 – evaldeslacasa 2017-10-30 22:23:14