public async Task <IEnumerable <IQPItem> > GetItemsAsync(bool forceRefresh = false) { Debug.WriteLine("GetItemsAsync enter"); if (forceRefresh) { GetDataResult = DownloadResult.Undefined; Debug.WriteLine("GetItemsAsync started, DownloadResult:" + GetDataResult); //Check network status if (NetworkServices.IsConnectedToInternet()) { try { //1. Download data Debug.WriteLine("GetItemsAsync download will start now, DownloadResult:" + GetDataResult); Uri geturi = new Uri(Settings.IQPURL); //replace your xml url HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(geturi); Debug.WriteLine("GetItemsAsync download completead, StatusCode:" + response.StatusCode); //2. If data ok if (response.StatusCode == System.Net.HttpStatusCode.OK) { //2.2. Read string string responseSt = await response.Content.ReadAsStringAsync(); //await ParentPage.DisplayAlert("Get IQP Data", responseSt, "Ok"); Debug.WriteLine("GetItemsAsync DOWNLOADDATA:" + responseSt); try { //3.1 Set JSON parse options JsonSerializerSettings JSONSettings = new JsonSerializerSettings(); JSONSettings.Culture = new CultureInfo("ru-RU"); JSONSettings.Culture.NumberFormat.NumberDecimalSeparator = "."; JSONSettings.NullValueHandling = NullValueHandling.Ignore; //3.2 Convert string into IQP Object List <IQPItem> IQP_items_list = JsonConvert.DeserializeObject <List <IQPItem> >(responseSt, JSONSettings); GetDataResult = DownloadResult.Success; Debug.WriteLine("GetItemsAsync Converted to JSON, Count:" + IQP_items_list.Count()); //Clear current items await ClearItemsAsync(); //Add test if needed if (_UseTestItems) { AddTestItems(); } //Add downloaded foreach (var _item in IQP_items_list) { await AddItemAsync(_item); } Debug.WriteLine("GetItemsAsync Data added into dataStoreIQPitems, Count:" + dataStoreIQPitems.Count()); Debug.WriteLine("GetItemsAsync DownloadResult:" + GetDataResult); } catch (Exception ex) { Debug.WriteLine("Exception in GetItemsAsync JSON parsing"); Debug.WriteLine(ex); GetDataResult = DownloadResult.ParseError; } } else { GetDataResult = DownloadResult.DownloadError; } } catch (Exception ex) { Debug.WriteLine("Exception in GetItemsAsync download"); Debug.WriteLine(ex); GetDataResult = DownloadResult.DownloadError; } } else { GetDataResult = DownloadResult.NoNetwork; } } return(await Task.FromResult(dataStoreIQPitems)); }
public static async Task <Tuple <T, DownloadResult> > GetJSON <T>(string stURL) { Debug.WriteLine("GetJSON [" + typeof(T) + "] enter"); //Return object T objResponse = default(T); DownloadResult retDataResult = DownloadResult.Undefined; // Check network status if (NetworkServices.IsConnectedToInternet()) { try { //1. Download data Uri geturi = new Uri(stURL); //replace your xml url HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(geturi); //2. If data ok if (response.StatusCode == HttpStatusCode.OK) { //2.2. Read string string responseSt = await response.Content.ReadAsStringAsync(); Debug.WriteLine("GetJSON [" + typeof(T) + "] DOWNLOAD DATA:" + responseSt); try { //3.1 Set JSON parse options JsonSerializerSettings JSONSettings = new JsonSerializerSettings(); JSONSettings.Culture = new CultureInfo("ru-RU"); JSONSettings.Culture.NumberFormat.NumberDecimalSeparator = "."; JSONSettings.NullValueHandling = NullValueHandling.Ignore; //3.2. JSON convert objResponse = JsonConvert.DeserializeObject <T>(responseSt, JSONSettings); Debug.WriteLine("GetJSON [" + typeof(T) + "] was converted to JSON:" + objResponse.ToString()); retDataResult = DownloadResult.Success; } catch (Exception ex) { Debug.WriteLine("Exception in GetJSON [" + typeof(T) + "] JSON parsing"); Debug.WriteLine(ex); retDataResult = DownloadResult.ParseError; } } else { retDataResult = DownloadResult.DownloadError; } } catch (Exception ex) { Debug.WriteLine("Exception in GetJSON [" + typeof(T) + "] download"); Debug.WriteLine(ex); retDataResult = DownloadResult.DownloadError; } } else { Debug.WriteLine("GetJSON [" + typeof(T) + "]- no network"); retDataResult = DownloadResult.NoNetwork; } Debug.WriteLine("GetJSON [" + typeof(T) + "] return status:" + retDataResult); return(new Tuple <T, DownloadResult>(objResponse, retDataResult)); }