繁体   English   中英

如何使用HttpClient Post方法在Xamarin中使用RestFull Web服务。

[英]How to Consuming RestFull Web Service in Xamarin using HttpClient Post Method.?

我正在使用Xamarin Cross-plateform创建简单的应用程序。 在第一页我输入数字并单击按钮。当用户单击按钮时,我想将此数字从Xamarin发送到Web服务,如果数字匹配,则在json中获取响应。

这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ScorpionTracker.Models
{
    public class DocketInfoModel
    {
        public string DocketNo { get; set; }
        public string PODScanFlag { get; set; }
        public string DocketDate { get; set; }
        public string FromToLoc { get; set; }
        public string ConsignorName { get; set; }
        public string ConsigneeName { get; set; }
        public string NoOfPackages { get; set; }
        public string ChargeWeight { get; set; }
        public string EDD_Date { get; set; }
        public string Delivered { get; set; }
        public string Status { get; set; }
        public string ErrorMessage { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string PODDocumentName { get; set; }
        public Boolean IsSuccess { get; set; }
    }
}

ScorpionLogin.xaml.cs

using ScorpionTracker.Models;
using System.Collections.Generic;
using System.Text;
using System;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Diagnostics;
using ScorpionTracker.ViewModels;
using ScorpionTracker.Util;

namespace ScorpionTracker.Views
{
    public partial class ScorpionLogin : ContentPage
    {
        DocketInfoModel docketInfoModel = new DocketInfoModel();
        public ScorpionLogin()
        {
            InitializeComponent();
        }
        async void onClick(object sender, EventArgs e)
        {
            docketInfoModel.DocketNo = DocketNo.Text;
            ScorpionTrackerViewModel getDocketDetail = new ScorpionTrackerViewModel(docketInfoModel);
            await Navigation.PushAsync(new ScorpionDocketDetails(docketInfoModel));
        }
    }
}

ScorpionTrackerViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using System.Diagnostics;
using System.ComponentModel;
using ScorpionTracker.Models;
using ScorpionTracker.Util;

namespace ScorpionTracker.ViewModels
{
    public class ScorpionTrackerViewModel
    {
        public DocketInfoModel _docketInfoModel;
        public string GET_DOCKET_DATA;

        public ScorpionTrackerViewModel(DocketInfoModel docketInfoModel)
        {
            this._docketInfoModel = docketInfoModel;
            getDocketDetails();
        }
        private async Task<DocketInfoModel> getDocketDetails()
        {
            // string strpost = "";
            var client = new HttpClient();
            client.BaseAddress = new Uri(ConstantData.BaseUrl);
            string docketno = _docketInfoModel.DocketNo;// this number i am sending from web service side.
            StringContent str = new StringContent(docketno, Encoding.UTF8, "application/x-www-form-urlencoded");

            //var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("DocketNo", docketno) });
            var response = await client.PostAsync(ConstantData.GET_DOCKET_DATA, str);
            var docketDataJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            DocketInfoModel docketinfomodel = new DocketInfoModel();
            if (docketDataJson != "")
            {
                docketinfomodel = JsonConvert.DeserializeObject<DocketInfoModel>(docketDataJson);
            }
            return docketinfomodel;
        }
    }
}

Complilation的结尾我得到Exception NameResoluationException。 我不知道如何解决这个我在谷歌搜索但没有得到任何解决方案。如果任何人知道评论请。 在此先感谢您的帮助。

这是一些事情。 您可能错误地将它们排除在外,但如果没有,那么您的问题可能就是其中之一。

  • 你在任何地方设置GET_DOCKET_DATA吗? 如果没有,那么您不会(通过我的假设)传递您的服务调用的完整URI。
  • 您的服务调用是否可以从浏览器(或其他可以像Fiddler一样进行POST的应用程序)工作? 需要确保您的端点正常工作。
  • 而不是PostAsync可能会尝试SendAsync 这是我用于POST和GET的那个。
  • 您正在构造函数中同步调用异步方法getDocketDetails 不要那样做。 构造函数用于内存分配,尤其不用于异步调用。 从构造函数中删除它并将您的方法更改为如下所示:
async void onClick(object sender, EventArgs e)
{
    docketInfoModel.DocketNo = DocketNo.Text;
    ScorpionTrackerViewModel viewmodel = new ScorpionTrackerViewModel(docketInfoModel);
    var docketDetail = await viewmodel.getDocketDetail()
    await Navigation.PushAsync(new ScorpionDocketDetails(docketDetail));
}

作为旁注,如果你将ScorpionTrackerViewModel设置为ScorpionLogin.xaml.cs的BindingContext,那么你没有手动新建它,你可以让按钮的Command直接连接到方法`getDocketDetails',假设你转换它公共命令。 但这只是清理模式的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM