Xamarin Forms-(IOS)System.Net.WebException已被抛出

时间:2017-06-02 09:21:29

标签: c# ios xamarin.ios xamarin.forms

Xamarin表单的新用户,我正在尝试将JSON数据和图片从网络检索到ListView,但我读到了 ATS https://developer.xamarin.com/guides/ios/platform_features/introduction_to_ios9/ats/我尝试在下面的info.plist中实现它是屏幕截图,但我仍然得到System.Net.WebException

我的图片

Image Screenshot

这是我得到的错误:

   $exception   {System.Net.WebException: 
    The resource could not be loaded because 
the App Transport Security policy …}    
System.Net.WebException

这是我的课程页面 OnlineStreaming.xaml.cs

public partial class OnlineStreaming : ContentPage
    {
        private HttpClient _client = new HttpClient();
        public ObservableCollection<Adverts> adverts;
        private const string Url = "http://eamobiledirectory.com/cooperp/Mobile/Mobileapi.aspx?Action=Featured&Country=Uganda";
        private const string BaseImageUrl = "http://eamobiledirectory.com/cooperp/Images/app_images/";

        public OnlineStreaming()
        {
            InitializeComponent();
        }




        protected override async void OnAppearing()
        {

            var content = await _client.GetStringAsync(Url);
            var adv = JsonConvert.DeserializeObject<List<Adverts>>(content);

            adverts = new ObservableCollection<Adverts>(adv);

            foreach (var item in adverts)
            {
                //some coding



                adverts.Add(new Adverts
                {

                    company_name = item.company_name
                                       ,
                    office_photo = BaseImageUrl + item.office_photo
                });

                //Debug.WriteLine("Output  is here  :" +  item.company_name);


            }



            listViewTest.ItemsSource = adverts;




        }
    }

我的Xaml页面:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="Online Streaming"
    x:Class="BBSTV.OnlineStreaming">



    <ListView  x:Name="listViewTest">
    <ListView.ItemTemplate>
    <DataTemplate>
                <ViewCell>
                    <StackLayout>
                    <Image  Aspect="Fill"  Source="{Binding office_photo}"/>
                        <Label  TextColor="Gray" Text="{Binding company_name}"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

请帮助我们,为什么这是IOS。

1 个答案:

答案 0 :(得分:0)

  

如果您的应用需要加载并显示来自非安全的网页内容   网站,将以下内容添加到您应用的Info.plist文件中以允许网络   Apple Transport Security(ATS)正确加载页面   仍然为应用的其余部分启用了保护:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

您需要将NSAllowsArbitraryLoadsInWebContent设置为true。 目前您已设置为false。

清理项目并再次运行