protected void CreateItems <U>(U reportObject, ReportType reportType) where U : class { try { ToggleBusy(true); Device.BeginInvokeOnMainThread(async() => { var client = await OAuthHelper.GetHTTPClientAsync(); var data = reportObject; string body = ""; try { body = JsonConvert.SerializeObject(data, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); } catch (Exception ex) { await DisplayAlert("Error", ex.Message, "Ok"); } if (string.IsNullOrEmpty(body)) { await DisplayAlert("Error", "There is no content to send! - serializtion failed", "Ok"); return; } var contents = new StringContent(body); contents.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); if (CheckConnection()) { var postResult = await client.PostAsync(SPUtility.GetListURL(reportType), contents); if (postResult.IsSuccessStatusCode) { lblLoading.Text = "Item created successfully." + Environment.NewLine; var spData = JsonConvert.DeserializeObject <SPData>(postResult.Content.ReadAsStringAsync().Result, new JsonSerializerSettings { DateParseHandling = DateParseHandling.None }); int itemId = spData.d.Id; await Task.Delay(500); await SendAttachments(itemId, reportType); MessagingCenter.Send <SSIRShortForm>(this, "home"); } else { var ex = await postResult.Content.ReadAsStringAsync(); await DisplayAlert("Error", ex, "Ok"); } } else { SPUtility.SaveOfflineItem(body, reportType, _attachementView.GetAttachmentInfoAsString()); await DisplayAlert("", "Item stored in local storage", "Ok"); MessagingCenter.Send <object>(this, "StartService"); MessagingCenter.Send(this, "home"); } ToggleBusy(false); }); } catch (HttpRequestException ex) { DependencyService.Get <IMessage>().ShortAlert("Upload Error"); } catch (Exception ex) { DependencyService.Get <IMessage>().ShortAlert("Upload Error" + ex.Message); } }
protected void CreateItems <U>(U reportObject) where U : class { try { ToggleBusy(true); Device.BeginInvokeOnMainThread(async() => { // StringContent contents = null; var client = await OAuthHelper.GetHTTPClientAsync(); var data = reportObject;// _viewobject; var body = JsonConvert.SerializeObject(data, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var contents = new StringContent(body); contents.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); if (CheckConnection()) { string url = SPUtility.GetListURL(ReportType.SationInfo); var postResult = await client.PostAsync(url, contents); if (postResult.IsSuccessStatusCode) { App.DAUtil.Delete(_StationInformation); lblLoading.Text = "Item created successfully." + Environment.NewLine; var spData = JsonConvert.DeserializeObject <SPData>(await postResult.Content.ReadAsStringAsync(), new JsonSerializerSettings { DateParseHandling = DateParseHandling.None }); int itemId = spData.d.Id; await Task.Delay(500); await SendAttachments(itemId); MessagingCenter.Send(this, "home"); } else { var ex = await postResult.Content.ReadAsStringAsync(); await DisplayAlert("Sharepoint error", ex, "Ok"); } ToggleBusy(false); } else { SaveOfflineItem(body, ReportType.SationInfo, _attachementView.GetAttachmentInfoAsString()); DependencyService.Get <IMessage>().LongAlert("Item stored in local storage"); ToggleBusy(false); } }); } catch (HttpRequestException ex) { DependencyService.Get <IMessage>().ShortAlert("Upload Error"); } catch (Exception ex) { DependencyService.Get <IMessage>().ShortAlert("Upload Error" + ex.Message); } }