在寫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){
//用Regex把各個元素分出來
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"));
}
}
作者“小帥之家”