1、获取国家的名称及代号
HttpClient httpClient = new HttpClient();
httpClient.setTimeout(10000);
String url = "http://www.google.com/ig/countries?output=xml&hl=zh-cn"; //中文
//String url = "http://www.google.com/ig/countries?output=xml&hl=en"; //英文
GetMethod mothod = new GetMethod(url);
mothod.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
int statusCode = httpClient.executeMethod(mothod);
if(statusCode == HttpStatus.SC_OK){
//通过dom4j解析出国家名称及国家代号
Document document = DocumentHelper.parseText(mothod.getResponseBodyAsString());
Element root = document.getRootElement();
List<Element> nodeList = root.selectNodes("//countries/country");
for(Element e: nodeList){
String countryName = e.element("name").attributeValue("data");
String countryCode = e.element("iso_code").attributeValue("data");
}
}
接口返回如下格式的数据:
<?xml version="1.0"?><xml_api_reply version="1"><countries><country><name data="美国"/><iso_code data="US"/></country><country><name data="英国"/><iso_code data="UK"/></country> ...... </countries></xml_api_reply>
2、获取一个国家的城市,其url地址如下:
String url = "http://www.google.com/ig/cities?hl=zh-cn&country=cn"; //中文
String url = "http://www.google.com/ig/cities?hl=en&country=cn"; //英文
接口返回如下格式的数据:
{code: "fr",cities: [{name: "昂蒂布", lat: 43580418, lon: 7125102},{name: "昂热", lat: 47478419, lon: -563166}, ...... ,{name: "亚眠", lat: 49894067, lon: 2295753}]}
3、获取一个城市的天气信息(需要根据城市的英文名称来获取),其url地址如下:
String url = http://www.google.com/ig/api?weather=Beijing&hl=zh-cn;
接口返回如下格式的数据:
<?xml version="1.0"?> <xml_api_reply version="1"> <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" > <forecast_information> <city data="Agra, Uttar Pradesh"/> <postal_code data="Agra"/> <latitude_e6 data=""/> <longitude_e6 data=""/> <forecast_date data="2012-03-16"/> <current_date_time data="1970-01-01 00:00:00 +0000"/> <unit_system data="SI"/> </forecast_information> <current_conditions> <condition data="晴"/> <temp_f data="81"/> <temp_c data="27"/> <humidity data="湿度: 23%"/> <icon data="/ig/images/weather/sunny.gif"/> <wind_condition data="风向: 西、风速:4 米/秒"/> </current_conditions> <forecast_conditions> <day_of_week data="周五"/> <low data="16"/> <high data="31"/> <icon data="/ig/images/weather/sunny.gif"/> <condition data="晴"/> </forecast_conditions> <forecast_conditions> <day_of_week data="周六"/> <low data="15"/> <high data="34"/> <icon data="/ig/images/weather/sunny.gif"/> <condition data="晴"/> </forecast_conditions> <forecast_conditions> <day_of_week data="周日"/> <low data="17"/> <high data="35"/> <icon data="/ig/images/weather/sunny.gif"/> <condition data="晴"/> </forecast_conditions> <forecast_conditions> <day_of_week data="周一"/> <low data="19"/> <high data="37"/> <icon data="/ig/images/weather/sunny.gif"/> <condition data="晴"/> </forecast_conditions> </weather> </xml_api_reply>
美国的天气需要使用城市的邮政编码来获取,比如 加州山景城的天气需要使用以下url地址获取其天气信息:
http://www.google.com/ig/api?hl=zh-cn&weather=94043