JAVA を使用して Google カスタム検索 API を使用するには、どのようにアプローチすればよいですか? Google 検索アプライアンス API の適切な使用法は何ですか?
4900 次
1 に答える
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();
}
}
于 2012-07-27T03:54:02.567 に答える