wol*_*e80 6 java search google-api-java-client
我正在尝试将Java客户端用于Google自定义搜索API, 但无法在网络上找到任何示例教程.有人能为我提供一个简单的例子吗?谢谢!
以下示例基于1-1.30 客户端库。由于没有太多文档,这绝对不是最好的例子。事实上,我故意使用一种已弃用的方法来设置 API 密钥,因为较新的方法似乎过于复杂。
假设您已在项目的构建路径中包含正确的 jar 依赖项,则基本示例如下:
//Instantiate a Customsearch object with a transport mechanism and json parser
Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory());
//using deprecated setKey method on customsearch to set your API Key
customsearch.setKey("YOUR_API_KEY_GOES_HERE");
//instantiate a Customsearch.Cse.List object with your search string
com.google.api.services.customsearch.Customsearch.Cse.List list = customsearch.cse().list("YOUR_SEARCH_STRING_GOES_HERE");
//set your custom search engine id
list.setCx("YOUR_CUSTOM_SEARCH_ENGINE_ID_GOES_HERE")
//execute method returns a com.google.api.services.customsearch.model.Search object
Search results = list.execute();
//getItems() is a list of com.google.api.services.customsearch.model.Result objects which have the items you want
List<Result> items = results.getItems();
//now go do something with your list of Result objects
Run Code Online (Sandbox Code Playgroud)
您需要从Google API 控制台获取自定义搜索引擎 ID 和 API 密钥
| 归档时间: |
|
| 查看次数: |
8189 次 |
| 最近记录: |