Android获取天气预报的两种方式(国家气象局和中央气象台天气预报API)

在项目中有一个天气预报的需求,在网上找了找大部分人给出的建议是使用webservice方式来获取数据。于是就尝试着做了一下,真是不做不知道,做了就收获了很多。

首先,来说一下通过webservice来获取国家气象局提供的天气服务

在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用WebService。适合手机的WebService客户端的SDK有一些,比较常用的是KSOAP2。(强烈建议大家去官网上下载http://code.google.com/p/ksoap2-android/。博主之前随便找了一个,结果是引用的sdk存在问题。坑死人啊。)注意,一定要下载正确的jar包。
http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.4/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar 点 View raw file 正确下载对应文件。



好了,下面直接上代码(这里只做简单的介绍)


public class MyWeather extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//展现天气预报的具体数据
		showWeather("北京");
			
	}
	//展现天气预报的具体数据
	private void showWeather(String city)
	{
		String weatherToday = null;
		int iconToday[] = new int[2];

		// 获取远程Web Service返回的对象
		SoapObject detail = WebServiceUtil.getWeatherByCity(city);// 根据城市获取城市具体天气情况
		// 获取天气实况
		weatherCurrent = detail.getProperty(4).toString();
		// 解析今天的天气情况
		String date = detail.getProperty(7).toString();
		weatherToday = "今天:" + date.split(" ")[0];
		weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
		weatherToday = weatherToday + "\n气温:"
			+ detail.getProperty(8).toString();
		weatherToday = weatherToday + "\n风力:"
			+ detail.getProperty(9).toString() + "\n";
		iconToday[0] = parseIcon(detail.getProperty(10).toString());
		iconToday[1] = parseIcon(detail.getProperty(11).toString());
		
	}

	// 工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。
	private int parseIcon(String strIcon)
	{
		if (strIcon == null)
			return -1;
		if ("0.gif".equals(strIcon))
			return R.drawable.a_0;
		if ("1.gif".equals(strIcon))
			return R.drawable.a_1;
		if ("2.gif".equals(strIcon))
			return R.drawable.a_2;
		if ("3.gif".equals(strIcon))
			return R.drawable.a_3;
		if ("4.gif".equals(strIcon))
			return R.drawable.a_4;
		if ("5.gif".equals(strIcon))
			return R.drawable.a_5;
		if ("6.gif".equals(strIcon))
			return R.drawable.a_6;
		if ("7.gif".equals(strIcon))
			return R.drawable.a_7;
		if ("8.gif".equals(strIcon))
			return R.drawable.a_8;
		if ("9.gif".equals(strIcon))
			return R.drawable.a_9;
		if ("10.gif".equals(strIcon))
			return R.drawable.a_10;
		if ("11.gif".equals(strIcon))
			return R.drawable.a_11;
		if ("12.gif".equals(strIcon))
			return R.drawable.a_12;
		if ("13.gif".equals(strIcon))
			return R.drawable.a_13;
		if ("14.gif".equals(strIcon))
			return R.drawable.a_14;
		if ("15.gif".equals(strIcon))
			return R.drawable.a_15;
		if ("16.gif".equals(strIcon))
			return R.drawable.a_16;
		if ("17.gif".equals(strIcon))
			return R.drawable.a_17;
		if ("18.gif".equals(strIcon))
			return R.drawable.a_18;
		if ("19.gif".equals(strIcon))
			return R.drawable.a_19;
		if ("20.gif".equals(strIcon))
			return R.drawable.a_20;
		if ("21.gif".equals(strIcon))
			return R.drawable.a_21;
		if ("22.gif".equals(strIcon))
			return R.drawable.a_22;
		if ("23.gif".equals(strIcon))
			return R.drawable.a_23;
		if ("24.gif".equals(strIcon))
			return R.drawable.a_24;
		if ("25.gif".equals(strIcon))
			return R.drawable.a_25;
		if ("26.gif".equals(strIcon))
			return R.drawable.a_26;
		if ("27.gif".equals(strIcon))
			return R.drawable.a_27;
		if ("28.gif".equals(strIcon))
			return R.drawable.a_28;
		if ("29.gif".equals(strIcon))
			return R.drawable.a_29;
		if ("30.gif".equals(strIcon))
			return R.drawable.a_30;
		if ("31.gif".equals(strIcon))
			return R.drawable.a_31;
		return 0;
	}
}

但是,多次运行项目之后,获取不到数据了。经过查找原来中国气象局提供的服务分为付费和免费的。而免费的每天有一定的访问次数限制。将应用安装到其他手机上也无济于事。并且免费的有时也不是很稳定,所以只能用其他服务了。


下面就来说一下访问中央气象台的天气预报API得到天气数据


使用这个服务唯一的缺点就是获取对应的城市码,这个有点麻烦(不过可以在这里下载http://download.csdn.net/tag/%E4%B8%AD%E5%9B%BD%E5%A4%A9%E6%B0%94%E7%BD%91%E5%9F%8E%E5%B8%82%E4%BB%A3%E7%A0%81%E6%95%B4%E7%90%86%E7%89%88.txt)其它的如稳定性与广阔性也是很一流的,它可以精确到县和区.下面就直奔主题:


这个服务的天气预报的请求地址是:http://m.weather.com.cn/data/101091106.html,这个文本就是城市天气URL,101091106代表的为对应地区的编码,执行URL,得到一个返回文本,是JSON格式的,如下(经过格式化):


{"weatherinfo":
			{"city":"北戴河",
			"city_en":"beidaihe",
			"date_y":"2013年7月31日",
			"date":"",
			"week":"星期三",
			"fchh":"18",
			"cityid":"101091106",
			"temp1":"23℃~28℃",
			"temp2":"23℃~27℃",
			"temp3":"25℃~28℃",
			"temp4":"25℃~30℃",
			"temp5":"25℃~29℃",
			"temp6":"25℃~28℃",
			"tempF1":"73.4℉~82.4℉",
			"tempF2":"73.4℉~80.6℉",
			"tempF3":"77℉~82.4℉",
			"tempF4":"77℉~86℉",
			"tempF5":"77℉~84.2℉",
			"tempF6":"77℉~82.4℉",
			"weather1":"晴转雷阵雨",
			"weather2":"小雨",
			"weather3":"中雨转小雨",
			"weather4":"小雨",
			"weather5":"多云转晴",
			"weather6":"晴转阴",
			"img1":"0",
			"img2":"4",
			"img3":"7",
			"img4":"99",
			"img5":"8",
			"img6":"7",
			"img7":"7",
			"img8":"99",
			"img9":"1",
			"img10":"0",
			"img11":"0",
			"img12":"2",
			"img_single":"4",
			"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":"西南风3-4级",
			"wind4":"微风",
			"wind5":"微风",
			"wind6":"东南风转西南风3-4级",
			"fx1":"微风",
			"fx2":"微风",
			"fl1":"小于3级",
			"fl2":"小于3级",
			"fl3":"3-4级",
			"fl4":"小于3级",
			"fl5":"小于3级",
			"fl6":"3-4级",
			"index":"热",
			"index_d":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",
			"index48":"舒适","index48_d":"建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
			"index_uv":"弱","index48_uv":"最弱",
			"index_xc":"不宜","index_tr":"一般","index_co":"较不舒适",
			"st1":"26","st2":"23","st3":"25","st4":"21","st5":"25","st6":"21",
			"index_cl":"较不宜","index_ls":"不宜","index_ag":"极不易发"
			}
}

部分参数说明:temp1~6表示六天的最低温和最高温、tempF1~tempF6:表示华氏温度
Img1~img2表示今天白天、晚上的天气图标、img3~img4表示明天白天、晚上的天气图标。。。

然后就是获取这些字符串了,这里就不介绍了。


主要向大家介绍一下jsonObject类(http://henzil.easymorse.com/?p=242)


JSONObject jsonObject = new JSONObject(jsonString)..getJSONObject("weatherinfo");其中的jsonString就是获取的字符串

例如得到天气图片
String imgurl = jsonObject.getString("img1");然后通过这个拼串就可以得到图片了
http://m.weather.com.cn/img/b+ imgurl +".gif"

上面就是两种方式获取天气情况了,欢迎大家给出意见或建议


评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值