Android weather forecast and Data Processing

Source: Internet
Author: User

Google weather api is used in the process of writing the android weather forecast program, while the data obtained by parsing the xml file with sax is in English. The problem of data conversion and display occurs here.
For example: Query weather http://www.google.com/ig/api in Wuhan? The xml data obtained by weather = wuhan is: <? 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 = "21:00:00 + 0000"/>
<Unit_system data = "SI"/>
</Forecast_information>
-<Current_conditions>
<Condition data = "Multi-cloud"/>
<Temp_f data = "72"/>
<Temp_c data = "22"/>
<Humidity data = "humidity: 78%" type = "humidity" text = "humidity"/>
<Icon data = "/ig/images/weather/cn_cloudy.gif"/>
<Wind_condition data = "wind direction: Northeast China, wind speed: 4 meters/second"/>
</Current_conditions>
+ <Forecast_conditions>
-<Forecast_conditions>
<Day_of_week data = "Saturday"/>
<Low data = "20"/>
<High data = "29"/>
<Icon data = "/ig/images/weather/chance_of_rain.gif"/>
<Condition data = "rain may exist"/>
</Forecast_conditions>
-<Forecast_conditions>
<Day_of_week data = "Sunday"/>
<Low data = "22"/>
<High data = "29"/>
<Icon data = "/ig/images/weather/chance_of_storm.gif"/>
<Condition data = "A storm may exist"/>
</Forecast_conditions>
-<Forecast_conditions>
<Day_of_week data = "Monday"/>
<Low data = "23"/>
<High data = "31"/>
<Icon data = "/ig/images/weather/chance_of_storm.gif"/>
<Condition data = "A storm may exist"/>
</Forecast_conditions>
</Weather>
</Xml_api_reply>
The data obtained after parsing with sax is:
Wuhan, Hubei
Overcast (Multi-cloud)
23:00:00 + 0000
Fri (Friday)
Wind: NE at 9 mph (indicating Wind direction and Wind speed)
Humidity: 87% (Humidity)
For display, the corresponding conversion is required. Now I have posted my conversion code, hoping to benefit the public.
Package com. xiaoshuai. util;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class WeatherUtils {
// Two functions are converted between Fahrenheit and Celsius. This is the code on the Internet.
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 ){
 
 
// Use a regular expression to separate each element
Pattern pattern = Pattern. compile ("");
String [] string = pattern. split (mph );
For (int I = 0; I <string. length; I ++ ){
System. out. println (string [I]);
}
// Convert the wind speed from a mile to a meter per second
Double distance = 1609.344;
Double mile = Integer. parseInt (string [3]);
Double mps = mile * distance/3600;
System. out. println (mps );
// Convert the wind speed into a level
String wind_class = "";
If (mps> = 0 <= 0.2 mps ){
Wind_class = "0 ";
} Else if (mps> = 0.3 & mps <= 1.5 ){
Wind_class = "Level 1 ";
} Else if (mps> = 1.6 & mps <= 3.3 ){
Wind_class = "Level 2 ";
} Else if (mps> = 3.4 & mps <= 5.4 ){
Wind_class = "Level 3 ";
} Else if (mps> = 5.5 & mps <= 7.9 ){
Wind_class = "Level 4 ";
} Else if (mps> = 8.0 & mps <= 10.7 ){
Wind_class = "5 ";
} Else if (mps> = 10.8 & mps <= 13.8 ){
Wind_class = "Level 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 = "grade 13 ";
}
// Convert the wind direction sign into Chinese
String direction = string [1];
String wind_direction = "";
If ("N". equals (direction )){
Wind_direction = "North Wind ";
} Else if ("W". equals (direction )){
Wind_direction = "West Wind ";
} Else if ("E". equals (direction )){
Wind_direction = "Dongfeng ";
} Else if ("S". equals (direction )){
Wind_direction = "Nanfeng ";
} Else if ("SE". equals (direction )){
Wind_direction = "southeast wind ";
} Else if ("NE". equals (direction )){
Wind_direction = "northeast wind ";
} Else if ("SW". equals (direction )){
Wind_direction = "SouthWest Wind ";
} Else if ("NW". equals (direction )){
Wind_direction = "northwest wind ";
}
Return wind_direction + wind_class;
}
Public static void main (String [] args ){
// If you input Wind: NE at 19 mph, the output is five levels of northeast Wind.
System. out. println (WeatherUtils. mph_to_mps ("Wind: NE at 19 mph "));
}
}

Author: "xiaoshuai home"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.