Ejemplo n.º 1
0
        private async void SetnReadOW(object sender, TappedRoutedEventArgs e)
        {
            //ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
            localSettings.Values["OWKey"] = _tboxOWKey.Text;
            localSettings.Values["OWLoc"] = _tboxOWLoc.Text;
            localSettings.Values["OWSet"] = 1;

            using (var client = new HttpClient())
            {
                string apikey = localSettings.Values["OWkey"].ToString();
                string wealoc = localSettings.Values["OWLoc"].ToString();
                string url    = "https://api.openweathermap.org/data/2.5/weather?id=" + wealoc + "&appid=" + apikey + "&units=metric&lang=en";
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                var response = await client.GetAsync(url);

                if (response.IsSuccessStatusCode)

                {
                    //Decode JSON to list here
                    var OWString = await response.Content.ReadAsStringAsync();

                    weatherJSon.Rootobject ro = JsonConvert.DeserializeObject <weatherJSon.Rootobject>(OWString);
                    client.Dispose();
                    _tbOWResult.Text = ro.name;
                }
                else
                {
                    _tbOWResult.Text = "Uhhhh!";
                }
            }
        }
Ejemplo n.º 2
0
        public async void DoTheMain()
        {
            //GC.Collect(2);

            var jsonSettings = new JsonSerializerSettings
            {
                NullValueHandling     = NullValueHandling.Include,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };

            using (var client = new HttpClient())
            {
                try
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    //var response = await client.GetAsync("https://api.openweathermap.org/data/2.5/weather?q=Marrickville,au&appid=1316159b5cc951e41bafa169cb346185&units=metric&lang=en");
                    var response = await client.GetAsync("https://api.openweathermap.org/data/2.5/weather?id=" + wealoc + "&appid=" + apikey + "&units=metric&lang=en");

                    if (response.IsSuccessStatusCode)
                    {
                        //Decode JSON to list here
                        var jsonString = await response.Content.ReadAsStringAsync();

                        weatherJSon.Rootobject ro = JsonConvert.DeserializeObject <weatherJSon.Rootobject>(jsonString);

                        //weatherJSon.Rootobject rootObject = new weatherJSon.Rootobject();
                        //AcList acList = new AcList();
                        //FlightList.ItemsSource = ro.acList;
                        string _weaName   = ro.name;
                        string _weaTemp   = ro.main.temp.ToString();
                        string _weaDesc   = ro.weather[0].description;
                        string _weaPress  = ro.main.pressure.ToString();
                        string _weaWinDir = ro.wind.deg.ToString();
                        string _weaWinSpd = ro.wind.speed.ToString();
                        string _weaHumid  = ro.main.humidity.ToString();
                        string _weaIcon   = "ms-appx:///Assets/images/weather/" + ro.weather[0].icon + ".png";
                        _TBbigTemp.Text     = _weaTemp + "°C ";
                        _TBbigwords.Text    = _weaDesc;
                        _TBbiglocation.Text = _weaName;
                        _TBPressure.Text    = _weaPress + "hPa";
                        _TBWDir.Text        = _weaWinDir + "°";
                        _TBWSpd.Text        = _weaWinSpd + "m/sec";
                        _TBHumid.Text       = _weaHumid + "%";
                        _imgWeather.Source  = new BitmapImage(new Uri(_weaIcon, UriKind.Absolute));
                    }
                    else
                    {
                        Debug.WriteLine("ERROR");
                        OnHTTPFail();
                    }
                }
                finally
                {
                    client.Dispose();
                }

                async void OnHTTPFail()
                {
                    var messageDialog = new MessageDialog("Error getting JSON file.");
                    await messageDialog.ShowAsync();
                }
            }
        }