跳到主要內容

發表文章

目前顯示的是 6月, 2012的文章

使用Google Custom Search REST API

介紹: 我使用v1 https://developers.google.com/custom-search/v1/overview 準備工作: 只要有Google 帳號就能申請,但一天只能免費查詢100次 申請設定custom search engine 並取得engine ID 申請Google API使用權限 並取得 API key 作法概念: 發出Http GET request 到下面的URL並設定好參數 https://www.googleapis.com/customsearch/v1?key=[INSERT-YOUR-API-KEY]&cx=[CUSTOM-SEARCH-ENGINE-ID] 範例程式: public String search(String keyword){ StringBuffer result = new StringBuffer(); try { HttpURLConnection connection = (HttpURLConnection)new URL(CSE_URL+"&q="+keyword).openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine())!=null){ result.append(line); } }catch (Exception e) { e.printStackTrace(); } return result.toString(); } 使用 Custom Search API - Java library : Javad