コード例 #1
0
        /// <summary>
        /// Gets all Locations from the servers REST Api.
        /// </summary>
        public async Task UpdateLocationsFromServer()
        {
            DbLocation db = new DbLocation();

            System.Diagnostics.Debug.WriteLine("LocationsController - UpdateLocationsFromServer: initiated");
            DbStudent dbStudent   = new DbStudent();
            string    accessToken = dbStudent.GetStudentAccessToken();

            if (accessToken == null)
            {
                Authenticater.Authorized = false;
                return;
            }

            Uri url = new Uri(Adress);

            System.Diagnostics.Debug.WriteLine("LocationsController - url " + url.ToString());
            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
            try
            {
                var response = await client.GetAsync(url);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    System.Diagnostics.Debug.WriteLine("UpdateLocationsFromServer response " + response.StatusCode.ToString());
                    var newLocations = await response.Content.ReadAsAsync <IEnumerable <Location> >();

                    db.DeleteAllLocations();
                    db.InsertLocations(newLocations);
                }
                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    Authenticater.Authorized = false;
                }
            }

            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("LocationsController - UpdateLocationsFromServer: await client.GetAsync(\"url\") Failed");
                System.Diagnostics.Debug.WriteLine("LocationsController - UpdateLocationsFromServer: Exception msg: " + e.Message);
                System.Diagnostics.Debug.WriteLine("LocationsController - UpdateLocationsFromServer: Stack Trace: \n" + e.StackTrace);
                System.Diagnostics.Debug.WriteLine("LocationsController - UpdateLocationsFromServer: End Of Stack Trace");
            }
        }