private async Task SendPostToDatabaseAsync(HomePagePost newPost) { try { PostButton.IsEnabled = false; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var newPost_JSON = new StringContent(JsonConvert.SerializeObject(newPost), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(new Uri("https://asp-impact.azurewebsites.net/api/HomePost"), newPost_JSON); string responseBody = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1] { '"' }); if (response.StatusCode == System.Net.HttpStatusCode.Accepted) { await PopupNavigation.PopAsync(); } else await DisplayAlert("Error", responseBody, "OK"); } } catch (Exception exception) { await DisplayAlert("Error", exception.Message, "OK"); } finally { PostButton.IsEnabled = true; } }
private async void Post_ButtonClicked(object sender, EventArgs e) { HomePagePost newPost = new HomePagePost() { creator_uid = App.currentUser.uid, posterName = App.currentUser.name, title = TitleEntry.Text, body = BodyEntry.Text, imageUrl = App.currentUser.imageUrl }; await SendPostToDatabaseAsync(newPost); }