2017-02-13 65 views
-1

我在爲基於Android的應用程序使用基於WCF的Web服務。以前,Web應用程序(爲其編寫Web服務)使用的是.NET Framework 3.5,最近它已遷移到.NET Framework 4.6。下面的代碼段被拋出異常:Xamarin.Android中是否支持.net Framework 4.6的Newtonsoft.JSON?

"Error: NameResolutionFailure at System.Net.HttpWebRequest.EndGetResponse"

url = https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1 

private async Task<JsonValue> FetchErrAsync(string url) 
     { 

      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); 
      request.ContentType = "application/json"; 
      request.Method = "GET"; 


      using (WebResponse response = await request.GetResponseAsync()) 
      { 


       using (Stream stream = response.GetResponseStream()) 
       { 

        JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream)); 

             return jsonDoc; 
       } 
      } 
      } 

web服務啓動和運行。 Json格式數據正在正常的Web瀏覽器中顯示,但是從android應用程序中,我們得到了上述例外。 注:此代碼工作正常時,此Web應用程序上運行在.NET Framework 3.5

+0

這是您通過NuGet包來安裝它 –

+0

是的,我有通過nuget包安裝newtonsoft.json。 –

+0

確保兩邊的對象屬性和大小寫順序相同,即您的應用和服務 –

回答

1

Is Newtonsoft.JSON supported for .net Framework 4.6 in Xamarin.Android?

是的它支持.net Framework 4.6在Xamarin.Android。

您可以將您的流轉換爲字符串,然後使用Newtonsoft.JSON將字符串轉換爲對象。

"Error: NameResolutionFailure at System.Net.HttpWebRequest.EndGetResponse"

這個錯誤不是關於Newtonsoft.JSON,它是關於網絡環境。通過測試你的網址。 (https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1),我發現證書的安全問題,我認爲你可以嘗試繞過ServerCertificateValidationCallback的繞過證書驗證,然後重試。

我有下面的代碼成功創建JSON字符串:

public class MainActivity : Activity 
    { 
     Button bt1; 
     TextView tv1; 
     TextView tv2; 
     TextView tv3; 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 
      bt1 = FindViewById<Button>(Resource.Id.button1); 
      tv1 = FindViewById<TextView>(Resource.Id.textView1); 
      tv2 = FindViewById<TextView>(Resource.Id.textView2); 
      tv3 = FindViewById<TextView>(Resource.Id.textView3); 
      bt1.Click += Bt1_Click; 
     } 

     private async void Bt1_Click(object sender, EventArgs e) 
     { 
      await FetchErrAsync("http://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1"); 
     } 
     public bool MyRemoteCertificateValidationCallback(System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
     { 
      bool isOk = true; 
      // If there are errors in the certificate chain, look at each error to determine the cause. 
      if (sslPolicyErrors != SslPolicyErrors.None) 
      { 
       for (int i = 0; i < chain.ChainStatus.Length; i++) 
       { 
        if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown) 
        { 
         chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain; 
         chain.ChainPolicy.RevocationMode = X509RevocationMode.Online; 
         chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0); 
         chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags; 
         bool chainIsValid = chain.Build((X509Certificate2)certificate); 
         if (!chainIsValid) 
         { 
          isOk = false; 
         } 
        } 
       } 
      } 
      return isOk; 
     } 
     private async Task FetchErrAsync(string url) 
     { 
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); 
      request.ContentType = "application/json"; 
      request.Method = "GET"; 
      ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; 
      using (WebResponse response = await request.GetResponseAsync()) 
      { 
       using (Stream stream = response.GetResponseStream()) 
       { 
        //JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream)); 
        //return jsonDoc; 
        StreamReader reader = new StreamReader(stream); 
        string text = reader.ReadToEnd(); 
        tv1.Text = text; 
        var myFetchNumberOfSEZandUnitsResultguage = JsonConvert.DeserializeObject<MyFetchNumberOfSEZandUnitsResultguage>(text); 
        tv2.Text = myFetchNumberOfSEZandUnitsResultguage.FetchNumberOfSEZandUnitsResult[0].Key; 
        tv3.Text = myFetchNumberOfSEZandUnitsResultguage.FetchNumberOfSEZandUnitsResult[0].Value; 
       } 
      } 
     } 


    } 
    public class MyFetchNumberOfSEZandUnitsResultguage 
    { 
     public List<MyKeyValue> FetchNumberOfSEZandUnitsResult { get; set; } 
    } 

    public class MyKeyValue 
    { 
     public string Key { get; set; } 
     public string Value { get; set; } 
    } 

屏幕截圖:

enter image description here

0

反序列化到一個特定的對象你的迴應,你可以使用:

NewtonSoft.Json.JsonConvert.DeserializeObject<MyClass>(webResponseInString); 

也是一大注:WCF ISN」 t完全支持Xamarin堆棧,所以在使用WCF時要小心。

+0

我已經按照你的建議使用了相同的反序列化格式,但仍然沒有運氣! –

+0

http://stackoverflow.com/questions/8999616/httpwebrequest-nameresolutionfailure-exception-in-net-with-mono-on-ubuntu – Mittchel

+0

感謝分享帖子Mittchel,但我們沒有使用主機名稱的網址,而是我們直接使用IP地址。 url = https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService。svc/FetchNumberOfSEZandUnits/1 這個問題不能由於我的網絡連接,因爲如果我使用舊的Web服務url(基於.net 3.5的web應用程序),代碼工作正常, –