How'd we make it this far without a weather application? I must admit its hard to buy anything these days that doesn’t have a weather feature built in. But does everyone understand the basic of writing such an application? That’s what we will explore today.
Mobile Weather
UI is a little plain. I really think a weather application needs to have a top notch exciting UI, but we’ll keep it simple this time. We have an input section at the top that takes the user’s ZIP Code, and we have an output section at the bottom.
In the output section we have some basic factoids like current conditions and temperature. We also have a forecast for the next day showing date, description, with high and low temperature. And finally we have a graphic to add a little something to an otherwise drab application.
How did we mine this information. Basically I connected to a public weather RSS feed that takes zip code as an URL parameter, using an HttpWebRespone object. I then parsed out the data I need using regular expressions.
There are other ways of doing this type of thing, but I thought this would allow us to use a lot of neat Window Mobile technologies we haven’t explorer before.
Our friend the HttpWebResponse:
1: private void RefreshData()
2: {
3: string lcUrl = "http://weather.yahooapis.com/forecastrss?p=" + textBoxZipCode.Text.TrimEnd();
4:
5: // *** Establish the request
6: HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
7:
8: // *** Set properties
9: loHttp.Timeout = 10000; // 10 secs
10:
11: // *** Retrieve request info headers
12: HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
13:
14: Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
15:
16: StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
17:
18: string lcHtml = loResponseStream.ReadToEnd();
19:
20: RefreshScreen(lcHtml);
21:
22: loWebResponse.Close();
23: loResponseStream.Close();
24: }
Last but not least the Regex object:
1: string conditionsEx = @"<yweather:condition text=""(?<desc>[^@]+)"" code=""(?<code>[^@]+)"" temp=""(?<temp>[^@]+)"" date=""(?<junk>[^@]+)"" ";
2:
3: Regex regEx = new Regex(conditionsEx);
4:
5: Match m = regEx.Match(input);
6:
7: if(m.Success)
8: {
9: desc = m.Groups["desc"].Value;
10: temp = m.Groups["temp"].Value;
11: }
Possibilities:
The UI needs more polish, but it is useable. There is even more data in this RSS feed than I’m exposing so that is another way to expand on this project and make something awesome.
Download executable: mobileWeather.cab
Download Source Code: mobileWeather.zip
Feedback:
How could we make this application even better? Share your ideas with the community.