Xamarin:Android应用程序不加载Https图像

时间:2018-02-03 14:35:29

标签: c# android xamarin xamarin.forms

我正在Xamarin中开发一个Multiplatform应用程序,我无法从android上的api加载https图像。如果我在Windows Phone中运行构建,那么在Android中加载图像的方式不是,所以我问,如何修复?

对不起英语,请使用谷歌翻译

Android - 打印 - 无图像

enter image description here

UWP enter image description here

https://pt.stackoverflow.com/questions/274026/xamarin-app-android-n%C3%A3o-carrega-imagens-https

Code Home.cs

 namespace AppNewsPlay.Views
{

    public partial class Home : TabbedPage
    {
        //private UltimasNoticias UltimasNoticias;
        List<UltimasNoticias> UNoticias;

        public TabbedPage Detail { get; private set; }

        public Home()
        {


            UNoticias = new List<UltimasNoticias>();
            ObterUltimasNoticias();
            InitializeComponent();

        }

        private async void ObterUltimasNoticias()
        {

            //   var jsonRequest = JsonConvert.SerializeObject(UNoticias);
            // var httpContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
            var resp = string.Empty;

            try
            {
                var uri = new HttpClient();
                uri.BaseAddress = new Uri("http://api.newsplay.com.br");
                var url = "/api/post/";
                var result = await uri.GetAsync(url);


                if (!result.IsSuccessStatusCode)
                {
                    await DisplayAlert("Erro de Conexão", "Não foi possível obter as notícias do servidor, Tente novamente mais tarde!", "OK");
                    return;
                }

                resp = await result.Content.ReadAsStringAsync();


            }
            catch (Exception ex)
            {
                await DisplayAlert("Erro de Conexão com o Servidor", ex.Message, "OK");
                return;

            }

            // transformando o retorno em objeto através do json e deserealize e retornando em lista
            var UNoticias = JsonConvert.DeserializeObject<List<UltimasNoticias>>(resp);

            //Adicionando os itens ao ListView na Home.xaml
            UnoticiasList.ItemsSource = UNoticias;

        }    

        private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if(e.SelectedItem !=null)
            {
                var selection = e.SelectedItem as UltimasNoticias;
                //DisplayAlert("Você Selecionou", selection.Post_title, "ok");         

                await Navigation.PushAsync(new PostView());
                #region DisabledSelectionHighlighting
                ((ListView)sender).SelectedItem = null;
                #endregion
            }



        }


    }
}

代码Xaml

  <TabbedPage.Children>

    <ContentPage Title="Ultimas Notícias" Icon="">
        <ContentPage.Content>
        <StackLayout
                    Spacing="20">      
                    <Label Text="Últimas Notícias"
                           FontSize="20"                        
                           FontAttributes="Bold"
                           VerticalTextAlignment="Center"
                           HorizontalOptions="Center"

                     />

            <ListView x:Name="UnoticiasList"
                              HasUnevenRows="True"      
                              SeparatorColor="White"
                              SeparatorVisibility="Default"
                              ItemSelected="OnItemSelected"
                              >


                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>

                                        <StackLayout
                                             Padding="20"                                          

                                             Orientation="Vertical"
                                            >

                                            <Image Source="{Binding Guid}"
                                                                       WidthRequest="250"
                                                                       HeightRequest="150"
                                                                       VerticalOptions="Center"

                                                                        />


                                            <Label x:Name="Post_title" Text="{ Binding Post_title }"
                                                               FontSize="16"
                                                               FontAttributes="Bold"  

                                                               HorizontalTextAlignment="Center"
                                                               />

                                            <Label x:Name="Post_content" Text="{ Binding Post_content }"
                                                   FontSize="12"                                                
                                                   HorizontalTextAlignment="Center"
                                                   VerticalTextAlignment="Center"
                                                   HeightRequest="70"

                                                   />

                                            <Label x:Name="Post_ad" Text="{Binding Post_ad}"
                                                   FontSize="11"                                            
                                                   HorizontalTextAlignment="Center"   
                                                   />                                        

                                        </StackLayout>                                    

                                </ViewCell>
                            </DataTemplate>                            
                        </ListView.ItemTemplate>                    
                    </ListView>
                </StackLayout>
        </ContentPage.Content>
    </ContentPage>


    <ContentPage Title="Xbox" Icon="">
        <ContentPage.Content>
        </ContentPage.Content>

    </ContentPage>

    <ContentPage Title="Playstation" Icon="">
        <ContentPage.Content>

        </ContentPage.Content>
    </ContentPage>

</TabbedPage.Children>

返回Json Api图片Https:

enter image description here

1 个答案:

答案 0 :(得分:0)

我解决了我的问题如下:

我注意到,在显示图像时,托管服务器会在之前创建重定向。

为了解决这个问题,我访问了我的api并更改了之前添加地址的返回,如下图所示,应用程序开始在Android上显示图像

enter image description here