Xamarin 如何发送带有我的条目的帖子

问题描述 投票:0回答:1

我需要在 Xamarin 中填写带有输入的表单,并将它们发送到我的 API 页面。

我已经尝试在“Postman”中发送数据并且它正确保存了它,但我想知道如何从Xamarin发送它。

注意,我可以从应用程序中正确读取数据。

public class FuelPurchase
{
    public int id { get; set; }
    public string date{ get; set; }
    public int vueltametal { get; set; }
    public int amount{ get; set; }
    public string station{ get; set; }
    public string location{ get; set; }
}

您在 Xamarin 中创建的表单就是这样。

<Label Text="Fuel Purchase"/>
            <Label Text="Fecha">
            <DatePicker x:Name="Date"/>

            <Label Text="Station"/>
            <Entry x:Name="Station"/>

            <Label Text="Location"/>
            <Entry x:Name="Location"/>

            <Label Text="Amount"/>
            <Entry x:Name="amount" Keyboard="Numeric"/>
c# xamarin xamarin.forms dotnet-httpclient
1个回答
1
投票

这是我用于 API 的静态类。如果您只有一个网址,则可以更改网址以匹配您的网址。确保单步执行并检查所有“/”是否位于正确的位置!

using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using NGJS = System.Text.Json;
using System.Threading.Tasks;

namespace TestBCS
{
    public class RestService
    {
        readonly HttpClient client;

        public RestService()
        {
            client = new HttpClient(new HttpClientHandler
            {
                Proxy = null,
                UseProxy = false
            });
        }

        #region GET
        public async Task<object> RefreshDataAsync(string url, string qs)
        {
            Uri uri = new Uri(string.Format(url, qs));

            HttpResponseMessage response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                using (var stream = await response.Content.ReadAsStreamAsync())
                {
                    return await NGJS.JsonSerializer.DeserializeAsync<object>(stream);
                }
            }
            //Error Handling here
            return null;
        }
        #endregion

        #region POST


        static void SerializeJsonIntoStream(object value, Stream stream)
        {
            using (var sw = new StreamWriter(stream, new UTF8Encoding(false), 1024, true))
            using (var jtw = new JsonTextWriter(sw) { Formatting = Formatting.None })
            {
                var js = new JsonSerializer();
                js.Serialize(jtw, value);
                jtw.Flush();
            }
        }

        HttpContent CreateHttpContent(object content)
        {
            HttpContent httpContent = null;

            if (content != null)
            {
                var ms = new MemoryStream();
                SerializeJsonIntoStream(content, ms);
                ms.Seek(0, SeekOrigin.Begin);
                httpContent = new StreamContent(ms);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }

            return httpContent;
        }

        public async Task PostStreamAsync(string url, object content)
        {
            string Url = url;

            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage(HttpMethod.Post, Url))
            using (var httpContent = CreateHttpContent(content))
            {
                request.Content = httpContent;

                using (var response = await client
                    .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
                    .ConfigureAwait(false))
                {
                    response.EnsureSuccessStatusCode();
                    Debug.WriteLine("Successfully Sent");
                }
            }
        }

        #endregion
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.