我正在尝试使用Xamarin.Forms框架构建跨平台应用程序。我一直在努力向我的应用程序显示一个虚拟的Restful API,但是当我启动以下页面时,应用程序会一直崩溃。请你帮助我好吗?如果我能弄清楚这一点,这对我来说将是一大步。谢谢!
这是我的观点页面: 的 ApiPage.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"
x:Class="MyApp.Views.ApiPage">
<StackLayout>
<StackLayout Orientation="Horizontal">
</StackLayout>
<ListView x:Name="postsListView">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
这是背后的代码: 的 ApiPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
public partial class ApiPage : ContentPage
{
private const string Url = "https://jsonplaceholder.typicode.com/posts";
private HttpClient _client = new HttpClient();
private ObservableCollection<Post> _posts;
public ApiPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
var content = await _client.GetStringAsync(Url);
var posts = JsonConvert.DeserializeObject<List<Post>>(content);
_posts = new ObservableCollection<Post>(posts);
postsListView.ItemsSource = _posts;
base.OnAppearing();
}
void OnAdd(object sender, System.EventArgs e)
{
}
void OnUpdate(object sender, System.EventArgs e)
{
}
void OnDelete(object sender, System.EventArgs e)
{
}
}
}
这是控制台输出,但没有说明任何内容:
11-04 18:12:09.430 D/Mono ( 4185): Probing 'java_interop_jnienv_call_boolean_method'.
11-04 18:12:09.431 D/Mono ( 4185): Found as 'java_interop_jnienv_call_boolean_method'.
11-04 18:12:09.521 W/art ( 4185): JNI RegisterNativeMethods: attempt to register 0 native methods for md5b60ffeb829f638581ab2bb9b1a7f4f3f.CellRenderer_RendererHolder
11-04 18:12:12.450 D/Mono ( 4185): Assembly Ref addref MyApp[0x9e1a6480] -> Newtonsoft.Json[0xb030b7c0]: 2
11-04 18:12:12.576 D/Mono ( 4185): Assembly Ref addref Mono.Security[0xad5563a0] -> System[0x9e1a78c0]: 14
An unhandled exception occured.
答案 0 :(得分:1)
似乎这是Ssl页面证书和Android操作系统之间的问题,您可以通过执行后续步骤来解决此问题
步骤2:验证您是否正在使用Native Http Handler和Native SSL / TLS impl。
1)双击Xamarin Android项目打开项目设置
2)在&#34;构建/ Android构建 - &gt;一般&#34;部分验证您是否具有正确的本机选项为&#34; HttpClient impl&#34; &安培; &#34; SSL / TLS impl&#34;如下