国内天气预报接口

中央气象台对外提供了一个查询天气预报的接口,通过该接口可以查询到县区一级的天气信息。获取天气信息的方法如下:

 

1、获取省份信息

public static String getCityCode(String url){
	try{
		HttpClient httpClient = new HttpClient();
		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){
			return mothod.getResponseBodyAsString();
		}
	}catch(Exception ex){
		ex.printStackTrace();
	}
	return null;
}

String provinceUrl = "http://m.weather.com.cn/data5/city.xml";
String provinceData = getCityCode(provinceUrl);
String[] provinceArray = provinceData.split(",");
for(String province: provinceArray){
	String[] provinceCodeArray = province.split("\\|");
	System.out.println(provinceCodeArray[0] + "," + provinceCodeArray[1]);
}

 结果类似如下:01|北京,02|上海,03|天津,04|重庆,05|黑龙江,06|吉林,07|辽宁,08|内蒙古,09|河北,10|山西,11|陕西,12|山东,13|新疆,14|西藏,15|青海,16|甘肃,17|宁夏,18|河南,19|江苏,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|贵州,27|四川,28|广东,29|云南,30|广西,31|海南,32|香港,33|澳门,34|台湾

 

2、获取市信息

private static void generateCityCode(OutputStreamWriter writer, String provinceCode) throws IOException {
	String townUrl = "http://m.weather.com.cn/data5/city"+provinceCode+".xml";
	String townData = getCityCode(townUrl);
	String[] townArray = townData.split(",");
	for(String town: townArray){
		String[] townCodeArray = town.split("\\|");
		System.out.println("  " + townCodeArray[0] + "," + townCodeArray[1]);
	}
}

 结果类似如下:2801|广州,2802|韶关,2803|惠州,2804|梅州,2805|汕头,2806|深圳,2807|珠海,2808|佛山,2809|肇庆,2810|湛江,2811|江门,2812|河源,2813|清远,2814|云浮,2815|潮州,2816|东莞,2817|中山,2818|阳江,2819|揭阳,2820|茂名,2821|汕尾

 

3、获取县区信息及其城市代码

private static void generateCountyCode(OutputStreamWriter writer,
		String provinceCode, String townCode) throws IOException {
	String countyUrl = "http://m.weather.com.cn/data5/city"+townCode+".xml";
	String countyData = getCityCode(countyUrl);
	String[] countyArray = countyData.split(",");
	for(String county: countyArray){
		String[] countyCodeArray = county.split("\\|");
		System.out.println("    " + countyCodeArray[0] + "," + countyCodeArray[1]);
		
		//城市代码
		String codeUrl = "http://m.weather.com.cn/data5/city"+countyCodeArray[0]+".xml";
		String codeData = getCityCode(codeUrl);
		String[] codeArray = codeData.split("\\|");
		System.out.println(codeArray[1]);
	}
}

 结果类似如下:280101|广州,280102|番禺,280103|从化,280104|增城,280105|花都

 

4、获取天气预报信息

private static void getWeather(String cityid){
	try{			
		String url = "http://m.weather.com.cn/data/"+cityid+".html";
		String jsonString = getCityCode(url);
		System.out.println(jsonString);
	}catch(Exception ex){
		ex.printStackTrace();
	}
}

   结果类似如下:

{"weatherinfo":{"city":"广州","city_en":"guangzhou","date_y":"2012年3月1日","date":"","week":"星期四","fchh":"08","cityid":"101280101","temp1":"18℃~12℃","temp2":"24℃~16℃","temp3":"24℃~19℃","temp4":"24℃~18℃","temp5":"22℃~18℃","temp6":"22℃~15℃","tempF1":"64.4℉~53.6℉","tempF2":"75.2℉~60.8℉","tempF3":"75.2℉~66.2℉","tempF4":"75.2℉~64.4℉","tempF5":"71.6℉~64.4℉","tempF6":"71.6℉~59℉","weather1":"小雨转雾","weather2":"雾","weather3":"雾转小雨","weather4":"阵雨转中到大雨","weather5":"中到大雨转中雨","weather6":"阵雨","img1":"7","img2":"18","img3":"18","img4":"99","img5":"18","img6":"7","img7":"3","img8":"22","img9":"22","img10":"8","img11":"3","img12":"99","img_single":"7","img_title1":"小雨","img_title2":"雾","img_title3":"雾","img_title4":"雾","img_title5":"雾","img_title6":"小雨","img_title7":"阵雨","img_title8":"中到大雨","img_title9":"中到大雨","img_title10":"中雨","img_title11":"阵雨","img_title12":"阵雨","img_title_single":"小雨","wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"舒适","index_d":"建议着薄型套装或牛仔衫裤等春秋过渡装。年老体弱者宜着套装、夹克衫等。","index48":"暖","index48_d":"建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。","index_uv":"最弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"舒适","st1":"18","st2":"13","st3":"25","st4":"16","st5":"25","st6":"20","index_cl":"较不宜","index_ls":"不宜","index_ag":"极易发"}}

 

 

你可能感兴趣的:(天气预报)