我在调用 API 时收到错误,它返回 HTML(发生错误)而不是导致 Xamarin 应用程序崩溃的 JSON。我意识到这对于 API 返回它并不理想,但我正在使用它。
Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:<。路径 '',第 0 行,第 0 位置。
IMyApi 类
Task<ApiResponse<ContentModel>> GetContentModel();
我的服务类
try
{
var response = await MyAPI.GetContentModel();
if (response.IsSuccessStatusCode)
{
return response.Content;
}
HandleApiErrors(response);
}
catch (Exception ex)
{
Debug.WriteLine(TAG, ex.ToString());
}
例外
Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:<。路径'',第 0 行,位置 0。在 <2073514815234917a5e8f91b0b239405> 中的 Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3]:0 在 Newtonsoft.Json.JsonTextReader.Read () [0x0004c] 在 <207351481015234917a5eb8f> 中.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] 在 <2073514815234917a5e8f91b0b239405> 中:/Refit/NewtonsoftJsonContentSerializer.cs:53 at Refit.RequestBuilderImplementation.DeserializeContentAsync[T] (System.Net.Http.HttpResponseMessage resp, System.Net.Http.HttpContent content) [0x002a8] in //Refit/RequestBuilderImplementation.cs:314 at Refit.RequestBuilderImplementation+<>c__DisplayClass14_0
2[T,TBody].<BuildCancellableTaskFuncForMethod>b__0 (System.Net.Http.HttpClient client, System.Threading.CancellationToken ct, System.Object[] paramList) [0x002be] in /_/Refit/RequestBuilderImplementation.cs:258 at ContentService.GetContent () [0x0002e] in /Users/ContentService.cs:28 : ContentService Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <2073514815234917a5e8f91b0b239405>:0 at Newtonsoft.Json.JsonSerializer.Deserialize[T] (Newtonsoft.Json.JsonReader reader) [0x00000] in <2073514815234917a5e8f91b0b239405>:0 at Refit.NewtonsoftJsonContentSerializer.DeserializeAsync[T] (System.Net.Http.HttpContent content) [0x000a6] in /_/Refit/NewtonsoftJsonContentSerializer.cs:53 at Refit.RequestBuilderImplementation.DeserializeContentAsync[T] (System.Net.Http.HttpResponseMessage resp, System.Net.Http.HttpContent content) [0x002a8] in /_/Refit/RequestBuilderImplementation.cs:314 at Refit.RequestBuilderImplementation+<>c__DisplayClass14_0
2[T,TBody].b__0(System.Net.Http.HttpClient 客户端,System.Threading.CancellationToken ct,System.Object[] paramList)[0x002be] in /_/Refit/RequestBuilderImplementation.cs :258