2016-08-19 52 views
1

我使用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。我不知道如何解決這個問題,我在谷歌搜索,但沒有得到任何解決辦法。如果有任何人知道評論。 在此先感謝您的幫助。

+0

NameResolutionException聽起來像您的服務器的DNS查找失敗 – Jason

+0

https://forums.xamarin.com/discussion/6783/system-net-webexception-name-resolution-error-when-calling-calling-wcf-services -in-mono-android-appl –

回答

0

以下是一些事情。你可能把它們遺漏了,但如果沒有,那麼你的問題可能就是其中之一。

  • 您是否在任何地方設置GET_DOCKET_DATA?如果沒有,那麼你沒有通過(我認爲是)你的服務呼叫的完整URI。
  • 您的服務調用是否可以從瀏覽器(或其他可以使POST成爲Fiddler的應用程序)工作?需要確保您的端點工作。
  • 而不是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作爲的BindingContext爲ScorpionLogin.xaml.cs那麼你不必手動將其更新,並且您可以將該按鈕的命令直接連接到方法`getDocketDetails',假設您將其轉換爲公共命令。但這只是一個清理模式的建議。

+0

「GET_DOCKET_DATA」只是由mistak添加。我已經添加了ConstatData.GET_DOCKET_DATA,我添加了WebService url。 –

+0

我也檢查了瀏覽器中的網址,它的工作正常。我使用SendAsync而不是PostAsync,但同樣的錯誤出現。 –