【问题标题】:Xamarin Forms PCL HttpClient throwing unhandled exception on AndroidXamarin Forms PCL HttpClient 在 Android 上引发未处理的异常
【发布时间】:2016-12-20 10:24:30
【问题描述】:

我在 Xamarin.Forms PCL 中直接使用 System.Net.Http.HttpClient。虽然它在 Windows Phone 上运行得非常好,但在 Android 上它会在 GetAsync 行上引发未处理的异常。

我缺少特定于平台的东西吗?

var client = new HttpClient();
var response = await client.GetAsync(Constants.ProjectsUri); // this breaks

【问题讨论】:

  • 实际的例外是什么?
  • 小心!!! Android 5 存在大量错误,如果您使用棒棒糖设备对其进行测试,请尝试将其作为发布版而不是调试版运行。
  • 而不是使用 await try .Result 这有时会改变我的代码的行为
  • Nooooooooo 不要听@MarioGalván。永远不要打电话给.Result,你正在引入你不想要的死锁。看看 ModernHttpClient 看看它是否可以帮助您解决问题。如果您提供了整个例外情况,将会有很大帮助。
  • 我有同样的问题,当与服务器的连接有限(例如超时、飞行模式)时会发生这种情况。由于 Mono 中的错误,Xamarin Insights 中也存在一个相关问题(如果您使用它,请更新它)。您找到其他解决方案了吗?

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


【解决方案1】:

使用原生 Android 客户端处理程序

在 Android 构建设置中,将 HttpClient Implementation 设置为使用 AndroidClientHandler

Link to Xamarin Documentation

Xamarin.Android 6.1 中引入了 AndroidClientHandler 类,为 Xamarin.Android 应用程序提供 TLS 1.2 支持。此类对所有 HTTP 连接使用本机 java.net.URLConnection,允许 HttpClient 实例使用 Android 可用的任何网络和加密协议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多