【问题标题】:Xamarin Forms Json Service Insert DataXamarin Forms Json 服务插入数据
【发布时间】:2016-12-12 07:28:08
【问题描述】:

我想在我的应用程序中向 json 服务添加一条记录。我怎样才能通过服务网址做到这一点。这是我的代码。

CustomerModel customer = new CustomerModel();
                customer.Name = entryCompanyName.Text;
                customer.Title = entryCompanyTitle.Text;
                customer.PhoneNumber = entryTelephone.Text;
                customer.FaxNumber = entryFax.Text;
                customer.Email = entryEmail.Text;
                customer.CityId = 6444;

                string json = JsonConvert.SerializeObject(customer);
                string sContentType = "application/json";
                string path = "service url";
                HttpClient Client = new HttpClient();
                var task = Client.PostAsync(path, new StringContent(json.ToString(), Encoding.UTF8, sContentType));

我正在尝试 M. Wiśnicki 的解决方案,但我遇到了这个错误

我在添加 System.net 时没有收到错误 :( 我在哪里出错了?

【问题讨论】:

  • 这段代码有什么问题?
  • 找不到这些方法; GetRequestStreamAsync GetResponseAsync

标签: c# json post xamarin xamarin.forms


【解决方案1】:

这对我有用

public static async Task<string> PostEntityToApi<T>(string yourMethodUrl, T yourModel)
  {
    try
    {
        if (_httpClient == null)
        {
           _httpClient = new HttpClient { BaseAddress = new Uri(yourWebSiteUrl) };
        }

        var stringContentInput = new StringContent(JsonConvert.SerializeObject(dto), Encoding.UTF8, "application/json");

        var response = await _httpClient.PostAsync(new Uri(yourWebSiteUrl. + apiUrl), stringContentInput);

        if (!response.IsSuccessStatusCode)
        {
            throw new Exception(response.StatusCode.ToString());
        }

        var stringAsync = await response.Content.ReadAsStringAsync();

        LoggingManager.Error("Received error response: " + stringAsync);

        return stringAsync;
    }
    catch (Exception exception)
    {
        return null;
    }
 }

【讨论】:

    【解决方案2】:

    您可以使用WebRequest,此示例对我有用,我在我的应用程序中使用它。

    这是 System.Net.WebRequest 类,在这里你可以找到doc

     public async Task<string> PostSample(object data, string uri)
        {
            // Create an HTTP web request using the URL:  
            var request = (HttpWebRequest) WebRequest.Create(new Uri(uri));
            request.ContentType = "application/json";
            request.Method = "POST";
            var itemToSend = JsonConvert.SerializeObject(data);
            using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))
            {
                streamWriter.Write(itemToSend);
                streamWriter.Flush();
                streamWriter.Dispose();
            }
    
            // Send the request to the server and wait for the response:  
            using (var response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:  
                using (var stream = response.GetResponseStream())
                {
                    var reader = new StreamReader(stream);
                    var message = JsonConvert.DeserializeObject<string>(reader.ReadToEnd());
                    return message;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-02-25
      • 1970-01-01
      • 2017-02-08
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多