如何使用JAVA使用Google Custom Search API? Google Search Appliance API的适当用途是什么?
答案 0 :(得分:3)
这是google自定义搜索API示例。你需要下载库,例子就在这里
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();
}
}