android天气预报、数据的处理
在写android天气预报程序的过程中用到google weather api,而用sax 解析xml文件得到的数据是英文的,这里就出现了数据的转化显示问题。
例如:查询武汉的天气http://www.google.com/ig/api?weather=wuhan得到的xml数据为: <?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="Wuhan, Hubei" />
<postal_code data="wuhan" />
<latitude_e6 data="" />
<longitude_e6 data="" />
<forecast_date data="2011-09-09" />
<current_date_time data="2011-09-09 21:00:00 +0000" />
<unit_system data="SI" />
</forecast_information>
- <current_conditions>
<condition data="多云" />
<temp_f data="72" />
<temp_c data="22" />
<humidity data="湿度: 78%" />
<icon data="/ig/images/weather/cn_cloudy.gif" />
<wind_condition data="风向: 东北、风速:4 米/秒" />
</current_conditions>
+ <forecast_conditions>
- <forecast_conditions>
<day_of_week data="周六" />
<low data="20" />
<high data="29" />
<icon data="/ig/images/weather/chance_of_rain.gif" />
<condition data="可能有雨" />
</forecast_conditions>
- <forecast_conditions>
<day_of_week data="周日" />
<low data="22" />
<high data="29" />
<icon data="/ig/images/weather/chance_of_storm.gif" />
<condition data="可能有暴风雨" />
</forecast_conditions>
- <forecast_conditions>
<day_of_week data="周一" />
<low data="23" />
<high data="31" />
<icon data="/ig/images/weather/chance_of_storm.gif" />
<condition data="可能有暴风雨" />
</forecast_conditions>
</weather>
</xml_api_reply>
而用sax解析后得到的数据为:
Wuhan, Hubei
Overcast (代表多云)
2011-09-09 23:00:00 +0000
Fri (代表周五)
Wind: NE at 9 mph(表示风向和风速)
Humidity: 87%(表示湿度)
用于显示则需要进行相应的转化。现把我的转化代码贴出来,希望能惠及大众。
package com.xiaoshuai.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WeatherUtils {
//华氏和摄氏温度之间转化的两个函数这是网上的代码
public static int fahrenheitToCelsius(int tFahrenheit) {
return (int) ((5.0f / 9.0f) * (tFahrenheit - 32));
}
public static int celsiusToFahrenheit(int tCelsius) {
return (int) ((9.0f / 5.0f) * tCelsius + 32);
}
public static String mph_to_mps(String mph){
//用正则表达式把各个元素分出来
Pattern pattern=Pattern.compile(" ");
String[] string=pattern.split(mph);
for(int i=0;i<string.length;i++){
System.out.println(string[i]);
}
//把风速由英里每小时转化成米每秒
double distance=1609.344;
double mile=Integer.parseInt(string[3]);
double mps=mile*distance/3600;
System.out.println(mps);
//把风速转化成等级
String wind_class="";
if(mps>=0&&mps<=0.2){
wind_class="0级";
}else if(mps>=0.3&&mps<=1.5){
wind_class="1级";
}else if(mps>=1.6&&mps<=3.3){
wind_class="2级";
}else if(mps>=3.4&&mps<=5.4){
wind_class="3级";
}else if(mps>=5.5&&mps<=7.9){
wind_class="4级";
}else if(mps>=8.0&&mps<=10.7){
wind_class="5级";
}else if(mps>=10.8&&mps<=13.8){
wind_class="6级";
}else if(mps>=13.9&&mps<=17.1){
wind_class="7级";
}else if(mps>=17.2&&mps<=20.7){
wind_class="8级";
}else if(mps>=20.8&&mps<=24.4){
wind_class="9级";
}else if(mps>=24.5&&mps<=28.4){
wind_class="10级";
}else if(mps>=28.5&&mps<=32.6){
wind_class="11级";
}else if(mps>=32.7&&mps<=36.9){
wind_class="12级";
}else if(mps>=37.0&&mps<=41.4){
wind_class="13级";
}
//把风向标志转化成汉语
String direction=string[1];
String wind_direction="";
if("N".equals(direction)){
wind_direction="北风";
}else if("W".equals(direction)){
wind_direction="西风";
}else if("E".equals(direction)){
wind_direction="东风";
}else if("S".equals(direction)){
wind_direction="南风";
}else if("SE".equals(direction)){
wind_direction="东南风";
}else if("NE".equals(direction)){
wind_direction="东北风";
}else if("SW".equals(direction)){
wind_direction="西南风";
}else if("NW".equals(direction)){
wind_direction="西北风";
}
return wind_direction+wind_class;
}
public static void main(String[] args) {
//如果把Wind: NE at 19 mph输入,则显示出:东北风5级
System.out.println(WeatherUtils.mph_to_mps("Wind: NE at 19 mph"));
}
}
作者“小帅之家”
补充:移动开发 , Android ,