Xamarin.Android:`Newtonsoft.Json.JsonReaderException`

Xamarin.Android: `Newtonsoft.Json.JsonReaderException`

运行 Visual Studio Mac 的当前预览频道,v8.3 build 1630,我的 Xamarin.Android 应用报告 Newtonsoft.Json.JsonReaderException.

下面的代码在 Visual Studio 的 Mac、v8.2.6(build 28)的当前稳定频道中运行良好。

我需要为 Mac v8.3 的 VS 更新一些东西吗?

代码

readonly static HttpClient _client = new HttpClient();

public static async Task<MyModel> GetModelFromApi()
{
    using (var response = await _client.GetAsync("https://my.api.url/endpoint").ConfigureAwait(false))
    {
        var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
        return JsonConvert.DeserializeObject<MyModel>(json);
    }
}

堆栈跟踪

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: �. Path '', line 0, position 0.Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: �. Path '', line 0, position 0.
  at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <12891e825fce44a581e5bbbb579c1d49>:0 
  at JsonParseTextRepro.ApiService.GetPun () [0x0013f] in <a23104cc471e42a78acea59e0c31e721>:0 
  at JsonParseTextRepro.App+DemoPage.OnAppearing () [0x00091] in <a23104cc471e42a78acea59e0c31e721>:0 

环境信息

=== Visual Studio Mac 企业版 2019(预览版)===

版本 8.3 预览版(8.3 内部版本 1630) 安装 UUID:6e5142b4-e8be-4d1c-b75e-4744b0d8c3de GTK+ 2.24.23(罗利主题) Xamarin.Mac 5.16.1.24 (d16-3 / 08809f5b)

=== Xamarin.Android ===

版本:10.0.0.40(Visual Studio 企业版) 提交:xamarin-android/d16-3/080eaac Android SDK:/Users/brandonm/Library/Developer/Xamarin/android-sdk-macosx 支持的 Android 版本: None 已安装

SDK工具版本:26.1.1 SDK平台工具版本:28.0.2 SDK 构建工具版本:28.0.3

构建信息: 单声道:mono/mono@6434153 Java.Interop: xamarin/java.interop@5836f58 LibZipSharp: grendello/LibZipSharp/d16-3@71f4a94 压缩包:nih-at/libzip@b95cf3f 混淆器:xamarin/proguard@905836d SQLite:xamarin/sqlite@8212a2d Xamarin.Android 工具:xamarin/xamarin-android-tools@cb41333

回答

这是托管链接器中的一个回归,它错误地删除了 Xamarin.Android.Net.AndroidClientHandler 库。

Xamarin 团队已意识到该问题,您可以在此处跟踪其状态: https://github.com/xamarin/xamarin-android/issues/3626

变通

要防止链接器删除 AndroidClientHandler 库,您可以将其添加到 Android Build 设置中的 Ignore assembles 选项。