未在httpclient.defaultrequestheaders.accept.add android xamarin中将对象引用设置为对象的实例

时间:2019-10-17 07:52:53

标签: xamarin

        if (CrossConnectivity.Current.IsConnected)
        {
            var client = new HttpClient();
            client.Timeout = TimeSpan.FromSeconds(200);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync("http://example.Com/Service1.svc/Findemployee?uid=" + Username.ToString());
            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();
                RootObject loginuserdata = JsonConvert.DeserializeObject<RootObject>(data);
                Strpassword = loginuserdata.FindemployeeResult;
            }


        }

在此行更新xamarin.forms之后抛出的错误是client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“ application / json”));

1 个答案:

答案 0 :(得分:0)

从Android 9(API级别28)开始,默认情况下禁用明文支持。

有关模式的详细信息,您可以检查Network security configuration

解决方案

创建文件 res / xml / network_security_config.xml -

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://example.Com/Service1.svc/Findemployee</domain>
    </domain-config>
</network-security-config>

AndroidManifest.xml -

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        ...>
        ...
    </application>
</manifest>