我正在尝试使用geonames API,但无法运行此处https://www.geonames.org/source-code/给出的示例代码。
WebService.setUserName("demo"); // add your username here
ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
searchCriteria.setQ("zurich");
ToponymSearchResult searchResult = WebService.search(searchCriteria);
for (Toponym toponym : searchResult.getToponyms()) {
System.out.println(toponym.getName()+" "+ toponym.getCountryName());
}
我总是收到以下错误消息:
WARNING: problems connecting to geonames server http://ws.geonames.org
java.net.UnknownHostException: ws.geonames.org
到目前为止,我已经尝试过搜索错误消息,但是我无法找出我必须在代码中进行哪些更改。
一些帮助解决此问题的方法。
编辑:已修复。以下对我有用:
WebService.setGeoNamesServer("api.geonames.org");
WebService.setUserName("demo"); // add your username here
ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
searchCriteria.setQ("zurich");
ToponymSearchResult searchResult = WebService.search(searchCriteria);
for (Toponym toponym : searchResult.getToponyms()) {
System.out.println(toponym.getName()+" "+ toponym.getCountryName());
}
感谢@Progman的帮助。