Google自定义搜索API [英] Google Custom Search API

查看:101
本文介绍了Google自定义搜索API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JAVA使用Google Custom Search API?
Google Search Appliance API的合适用途是什么?

How should one approach on using Google Custom Search API using JAVA? What is the appropriate use of Google Search Appliance API?

推荐答案

这里是google自定义搜索API示例。你需要下载库和例子在这里

Here is google custom search API Example. you need to download Libraries and example is here

public class Custom {
public static void main(String args[]) throws IOException  {
        String key="Replace with your API key";
        String qry="batman";// search key word
        URL url = new URL(
                "https://www.googleapis.com/customsearch/v1?               
                key="+key+"&cx="Replace with unique ID"&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(url);
        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);
            }     
        }
        conn.disconnect();   
    }

  }

这篇关于Google自定义搜索API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆