2017-02-13 63 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应用程序),代码工作正常, –