我使用Xamarin Cross-plateform創建簡單的應用程序。在第一頁我輸入數字並點擊按鈕。當用戶單擊按鈕比我想從Xamarin發送這個數字到Web服務,如果數量匹配比在JSON中得到響應。如何使用HttpClient Post方法在Xamarin中使用RestFull Web服務。
這是我的代碼。
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我得到異常NameResoluationException。我不知道如何解決這個問題,我在谷歌搜索,但沒有得到任何解決辦法。如果有任何人知道評論。 在此先感謝您的幫助。
NameResolutionException聽起來像您的服務器的DNS查找失敗 – Jason
https://forums.xamarin.com/discussion/6783/system-net-webexception-name-resolution-error-when-calling-calling-wcf-services -in-mono-android-appl –