Google天气预报API

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

   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值