コード例 #1
0
        public override WeatherNodes GetXMLForWeather(WeatherLocation i_Location)
        {
            if (!WeatherCache.ContainsKey(i_Location.City))
            {
                WeatherCache.Add(i_Location.City, base.GetXMLForWeather(i_Location));
            }

            return(WeatherCache[i_Location.City]);
        }
コード例 #2
0
        private WeatherLocation getDefaultLocation()
        {
            WeatherLocation location = new WeatherLocation();

            location.City           = DefaultCity;
            location.Country        = DefaultCountry;
            location.CountryIsoCode = GetCountryIso(DefaultCountry);

            return(location);
        }
コード例 #3
0
        public virtual 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)
                            {
                                throw new WeatherException(string.Format("Could not find selected event location! setting to default values({0}, {1}", DefaultCity, DefaultCountry));
                            }
                        }
                    }
                    else
                    {
                        throw ex;
                    }
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }

            return(loadedObj);
        }
コード例 #4
0
 private void getLocationForWeather(Event i_SelectedEvent, out WeatherLocation i_Location)
 {
     i_Location = new WeatherLocation();
     if (i_SelectedEvent.Place.Location != null)
     {
         i_Location.City    = i_SelectedEvent.Place.Location.City;
         i_Location.Country = i_SelectedEvent.Place.Location.Country;
         try
         {
             i_Location.CountryIsoCode = GetCountryIso(i_Location.Country);
         }
         catch (Exception)
         {
             throw new WeatherException(string.Format("Could not find selected event location! setting to default values({0}, {1}", DefaultCity, DefaultCountry));
         }
     }
     else
     {
         throw new Exception("Missing location for the event!");
     }
 }