コード例 #1
0
        public WeatherNodes GetXMLForWeather(WeatherLocation i_Location)
        {
            bool          foundWeather  = false;
            XmlReader     reader        = null;
            XmlSerializer serializerObj = null;
            WeatherNodes  loadedObj     = null;

            ///Try with the event location first,if not found get the default
            while (foundWeather == false)
            {
                string url = string.Format(@"http://api.openweathermap.org/data/2.5/forecast?q={0},{1}&units=metric&mode=xml&appid=d6c795304f427b8b78559ab660464d80", i_Location.City, i_Location.CountryIsoCode);
                try
                {
                    reader        = XmlReader.Create(url);
                    foundWeather  = true;
                    serializerObj = new XmlSerializer(typeof(WeatherNodes));
                    loadedObj     = (WeatherNodes)serializerObj.Deserialize(reader);
                }
                catch (Exception ex)
                {
                    ////Handle case for city not found from this API:
                    if (ex is WebException)
                    {
                        WebException webExp = ex as WebException;

                        if (!(webExp == null) && ((WebException)ex).Status == WebExceptionStatus.ProtocolError)
                        {
                            HttpWebResponse resp = webExp.Response as HttpWebResponse;

                            if (resp != null && resp.StatusCode == HttpStatusCode.NotFound)
                            {
                                MessageBox.Show(string.Format("Sorry could not get weather for {0} fetching weather for {1} instead)", i_Location.City, k_defaultCity));
                                ResetToDefaultCity(i_Location);
                            }
                        }
                    }
                    else
                    {
                        throw ex;
                    }
                }
                finally
                {
                    reader.Close();
                }
            }

            return(loadedObj);
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: BenMosheGuyAgiv/EX02
        private void getWeatherForEvent(Event i_SelectedEvent)
        {
            WeatherLocation location = new WeatherLocation();

            getLocationForWeather(i_SelectedEvent, location);
            WeatherNodes loadedObj    = FacebookManager.GetXMLForWeather(location);
            Weather      eventWeather = null;

            foreach (Weather obj in loadedObj.WeatherList)
            {
                if (i_SelectedEvent.StartTime >= obj.FromTime && i_SelectedEvent.StartTime <= obj.ToTime)
                {
                    eventWeather = obj;
                }
            }

            recommendForWeather(i_SelectedEvent, eventWeather);
            pictureBoxWeatherIcon.Load(string.Format("http://openweathermap.org/img/w/{0}.png", eventWeather.WeatherSymbol.WeatherCode));
            pictureBoxEvent.Load(i_SelectedEvent.PictureLargeURL);
            panelEventDetails.Visible = true;
        }