Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.Android中的.net Framework 4.6是否支持Newtonsoft.JSON?_Android_.net_Json_Web Services_Xamarin - Fatal编程技术网

Xamarin.Android中的.net Framework 4.6是否支持Newtonsoft.JSON?

Xamarin.Android中的.net Framework 4.6是否支持Newtonsoft.JSON?,android,.net,json,web-services,xamarin,Android,.net,Json,Web Services,Xamarin,我正在为android应用程序使用基于WCF的web服务。以前web应用程序(已经为其编写了web服务)使用.NET framework 3.5,最近它被迁移到.NET framework 4.6。以下代码段引发异常: “错误:位于的NameResolutionFailure” System.Net.HttpWebRequest.EndGetResponse“ url=https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.

我正在为android应用程序使用基于WCF的web服务。以前web应用程序(已经为其编写了web服务)使用.NET framework 3.5,最近它被迁移到.NET framework 4.6。以下代码段引发异常:

“错误:位于的NameResolutionFailure” System.Net.HttpWebRequest.EndGetResponse“

url=https://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1
专用异步任务FetchErrAsync(字符串url)
{
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(新Uri(url));
request.ContentType=“application/json”;
request.Method=“GET”;
使用(WebResponse=await request.GetResponseAsync())
{
使用(Stream=response.GetResponseStream())
{
JsonValue jsonDoc=wait Task.Run(()=>JsonObject.Load(stream));
返回jsonDoc;
}
}
}
Web服务已启动并正在运行。Json格式的数据显示在普通的web浏览器中,但是从android应用程序中,我们得到了上述例外。
注意:当web应用程序在.NET framework 3.5上运行时,此代码工作正常,可以反序列化您对特定对象的响应,您可以使用:

NewtonSoft.Json.JsonConvert.DeserializeObject<MyClass>(webResponseInString);
NewtonSoft.Json.JsonConvert.DeserializeObject(WebResponseInstance);
还有一个重要的注意事项:Xamarin堆栈中不完全支持WCF,因此在使用WCF时要小心

Xamarin.Android中的.NETFramework 4.6是否支持
Newtonsoft.JSON

是的,Xamarin.Android中的.net Framework 4.6支持它

您可以将流转换为字符串,然后使用
Newtonsoft.JSON
将字符串转换为对象

“错误:System.Net.HttpWebRequest.EndGetResponse上的NameResolutionFailure”

此错误与
Newtonsoft.JSON
无关,而是与网络环境有关。通过测试你的url。(),我发现证书存在安全问题,我认为您可以尝试使用
服务器CertificateValidationCallback
绕过证书验证,然后重试

我已通过以下代码成功获取您的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; }
    }
公共类main活动:活动
{
按钮bt1;
文本视图tv1;
文本视图tv2;
文本视图tv3;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
bt1=FindViewById(Resource.Id.button1);
tv1=FindViewById(Resource.Id.textView1);
tv2=FindViewById(Resource.Id.textView2);
tv3=findviewbyd(Resource.Id.textView3);
bt1.点击+=bt1\u点击;
}
私有异步无效Bt1_单击(对象发送方,事件参数e)
{
等待获取同步(“http://121.242.223.199/SEZOnlineWebService/SezOnlineWebService.svc/FetchNumberOfSEZandUnits/1");
}
public bool MyRemoteCertificateValidationCallback(System.Object发送方、System.Security.Cryptography.X509Certificates.X509Certificate证书、X509Chain链、SslPolicyErrors SslPolicyErrors)
{
bool-isOk=true;
//如果证书链中存在错误,请查看每个错误以确定原因。
如果(sslPolicyErrors!=sslPolicyErrors.None)
{
对于(int i=0;iJsonObject.Load(stream));
//返回jsonDoc;
StreamReader=新的StreamReader(流);
string text=reader.ReadToEnd();
tv1.Text=文本;
var myFetchNumberOfSEZandUnitsResultguage=JsonConvert.DeserializeObject(文本);
tv2.Text=myFetchNumberOfSEZandUnitsResultguage.FetchNumberOfSEZandUnitsResult[0]。键;
tv3.Text=myFetchNumberOfSEZandUnitsResultguage.FetchNumberOfSEZandUnitsResult[0]。值;
}
}
}
}
公共类MyFetchNumberOfSEZandUnitsResultguage
{
公共列表FetchNumberOfSEZandUnitsResult{get;set;}
}
公共类MyKeyValue
{
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
屏幕截图:
 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; }
    }