public MainWindow() { forecast = null; client = new HttpClient(); InitializeComponent(); getForecast(); }
static async Task <ForecastKota> getForecasteKotaAsync(String path) { ForecastKota forecastKota = null; HttpResponseMessage respon = await client.GetAsync(path); if (respon.IsSuccessStatusCode) { forecastKota = await respon.Content.ReadAsAsync <ForecastKota>(); } return(forecastKota); }
async Task getForecast() { client.BaseAddress = new Uri("http://api.openweathermap.org/data/2.5/weather"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //string kota = "Malang"; setKota(); /* string kota = await getKota();*/ string kota = "Malang"; string key = "{YOUR API KEY}"; string path = "?q=" + kota + "&APPID=" + key; try { //GET forecast = await getForecasteKotaAsync(path); } catch (Exception e) { Console.WriteLine(e.Message); } }