Beispiel #1
0
		/// <summary>
		/// Gets the weather.
		/// </summary>
		/// <returns>The weather.</returns>
		private async Task<Weather.WeatherData> GetWeather()
		{
			var location = new Weather.Location ();
			WeatherData weather = new WeatherData ();

			// Get current location
			try {
				location = await LocationClient.GetLocation ();
			} catch (WebException e) {
				ShowNotification ("Error", "Error defining current location");
				// TODO: show view to enter current location
			} catch (Exception e) {
				ShowNotification ("Error", "Application will be closed.");
				Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
			}

			var storedLocations = await ConfigurationHelper.GetStoredLocations ();
			foreach(var loc in storedLocations)
			{
				var localWeather = await WeatherClient.GetWeather (new Weather.Location{ city = loc});
				if (localWeather != null)
					CurrentWeather.Add (localWeather);
			}

			return weather;
		}
		/// <summary>
		/// Performs web request and gets the weather from the service.
		/// </summary>
		/// <returns>The weather.</returns>
		/// <param name="location">Location to get the weather</param>
		public static async Task<WeatherData> GetWeather(Location location)
		{
			WeatherData result = null;
			string weatherUrl = string.Format (WEATHER_URL, EnumHelper.GetDescription (Subscription.Subscript.free), location.city, NUMBER_OF_DAYS.ToString (), ApplicationSettingsHelper.API_KEY);
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(weatherUrl);

			using(WebResponse response = await request.GetResponseAsync())
			{
				using (Stream resStream = response.GetResponseStream ()) 
				{
					using (StreamReader reader = new StreamReader (resStream)) 
					{
						string jsonResult = await reader.ReadToEndAsync ();
						result = JsonConvert.DeserializeObject<WeatherData> (jsonResult);
					}
				}
			}
			 
			int  length1 = result.data.weather.Length;
			int  length2 = result.data.weather[0].hourly.Length;

			// TODO: investigate ho w to run this tasks in Parallel
			for( int i = 0; i < length1; i++){
				for(int j = 0; j < length2; j++) {
					//taskArray[i*length2 + j]  = Task.Factory.StartNew(async () => { await GetWeatherImageFile (result.data.weather[i].hourly[j].weatherIconUrl[0].value); });
					await GetWeatherImageFile (result.data.weather[i].hourly[j].weatherIconUrl[0].value);
				}
			}
			return result;
		}