返回从Json创建的对象时的unhandeled异常

时间:2017-03-17 18:35:32

标签: json rest xamarin xamarin.forms

大家好,我一直在Xamarin的这个跨平台应用程序上工作,我的数据库托管在heroku。要对用户进行身份验证,我正在运行http客户端调用并获取包含我的身份验证令牌的json作为我的响应。我正在做出正确的请求并获得正确的响应,但是当我尝试返回对象时,我得到了一个无法解决的异常,我不知道为什么。我周围看了很多,似乎没有多少和我有同样的问题,所以我确信我只是做了一些愚蠢的错误。此外,我不确定为什么任何异常都没有被catch块捕获。如果你可以帮助我看看我的方向,我会出错,这将是伟大的。谢谢!

这是我的http电话代码:

public async Task<User> AuthenticateuserAsync(User user)
    {
        var uri = new Uri(string.Format(Constants.authenticationURL, string.Empty));
        var json = JsonConvert.SerializeObject(user);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        try
        {
            HttpResponseMessage response = null;
            response = await client.PostAsync(uri, content);
            if (response.IsSuccessStatusCode)
            {
                Debug.WriteLine(@"          Response is successful");
                var temp = await response.Content.ReadAsStringAsync();
                //dynamic result = JsonConvert.DeserializeObject<dynamic>(temp);
                //authUser = new User(result.key);
                //Debug.WriteLine(authUser.key);
                User authUser = JsonConvert.DeserializeObject<User>(temp);
                return authUser;
            }
            else
            {
                Debug.WriteLine(@"          Response failed with " + response.StatusCode);    
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(@"              ERROR authenticating {0}", ex.InnerException.Message);
        }
        return null;
    }

这是我调用服务的代码:

async void OnLoginButtonClicked(object sender, EventArgs e)
    {
        User user = new User(usernameEntry.Text, passwordEntry.Text);
        user = await AreCredentialsCorrect(user);
        if (user != null)
        {
            Navigation.InsertPageBefore(new NavigationXaml(user), this);
            await Navigation.PopAsync();
        }
        else
        {
            messageLabel.Text = "Login failed!";
            passwordEntry.Text = string.Empty;
        }
    }

    async Task<User> AreCredentialsCorrect(User user)
    {
        user = await obj.AuthenticateuserAsync(user);
        return user;
    }

这是我的用户类:

public class User
{
    [JsonProperty(Required = Required.AllowNull)]
    public string key { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string ID { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string username { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string password { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string email { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string FirstName { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string LastName { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string Language { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string SecurityQuestion { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string Answer { get; set; }
    [JsonProperty(Required = Required.AllowNull)]
    public string ProfilePic { get; set; }

    public User()
    {
    }

    public User(string username, string password)
    {
        this.username = username;
        this.password = password;
        email = string.Empty;
    }

    public User(string username, string password, string email)
    {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public User(string id, string email, string firstname, string lastname)
    {
        ID = id;
        this.email = email;
        FirstName = firstname;
        LastName = lastname;
    }
}

我的json回复是:

{
    "key": "d4690ee898d67bbcfcf2cac75b08ebc553ebcfc5"
}

输出日志是:

[0:] Response is successful 03-17 18:20:42.554 D/Mono ( 4485): Image addref Mono.CSharp[0xee38ad60] -> Mono.CSharp.dll[0xd3e1e500]: 2 03-17 18:20:42.554 D/Mono ( 4485): Prepared to set up assembly 'Mono.CSharp' (Mono.CSharp.dll) 03-17 18:20:42.554 D/Mono ( 4485): Assembly Mono.CSharp[0xee38ad60] added to domain RootDomain, ref_count=1 03-17 18:20:42.555 D/Mono ( 4485): AOT module 'Mono.CSharp.dll.so' not found: dlopen failed: library "/data/app/App1.Droid-1/lib/x86/libaot-Mono.CSharp.dll.so" not found 03-17 18:20:42.555 D/Mono ( 4485): AOT module '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/lib/mono/aot-cache/x86/Mono.CSharp.dll.so' not found: dlopen failed: library "/data/app/App1.Droid-1/lib/x86/libaot-Mono.CSharp.dll.so" not found 03-17 18:20:42.555 D/Mono ( 4485): Config attempting to parse: 'Mono.CSharp.dll.config'. 03-17 18:20:42.555 D/Mono ( 4485): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/Mono.CSharp/Mono.CSharp.config'. 03-17 18:20:42.555 D/Mono ( 4485): Assembly Ref addref Microsoft.CSharp[0xd5caa300] -> Mono.CSharp[0xee38ad60]: 2 03-17 18:20:42.555 D/Mono ( 4485): Assembly Ref addref Mono.CSharp[0xee38ad60] -> mscorlib[0xeb1e98c0]: 47 03-17 18:20:42.556 D/Mono ( 4485): Assembly Ref addref Mono.CSharp[0xee38ad60] -> System.Core[0xeb1f51e0]: 12 Loaded assembly: Mono.CSharp.dll [External] 03-17 18:20:42.615 D/Mono ( 4485): Assembly Ref addref Mono.CSharp[0xee38ad60] -> System[0xeb1ea8e0]: 16 03-17 18:20:43.014 D/Mono ( 4485): Assembly Ref addref App1.Droid[0xeb1e9a40] -> System[0xeb1ea8e0]: 17 03-17 18:20:43.018 D/Mono ( 4485): Unloading image mscorlib.dll [0xd3e1ef00]. 03-17 18:20:43.018 D/Mono ( 4485): Config attempting to parse: 'mscorlib.dll.config'. 03-17 18:20:43.018 D/Mono ( 4485): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/mscorlib/mscorlib.config'. 03-17 18:20:43.018 D/Mono ( 4485): Assembly Ref addref App1[0xeb1e9980] -> mscorlib[0xeb1e98c0]: 48 03-17 18:20:43.022 D/Mono ( 4485): Image addref System.Diagnostics.Tools[0xee38ae20] -> System.Diagnostics.Tools.dll[0xd3e1ef00]: 2 03-17 18:20:43.023 D/Mono ( 4485): Prepared to set up assembly 'System.Diagnostics.Tools' (System.Diagnostics.Tools.dll) 03-17 18:20:43.023 D/Mono ( 4485): Assembly System.Diagnostics.Tools[0xee38ae20] added to domain RootDomain, ref_count=1 03-17 18:20:43.024 D/Mono ( 4485): AOT module 'System.Diagnostics.Tools.dll.so' not found: dlopen failed: library "/data/app/App1.Droid-1/lib/x86/libaot-System.Diagnostics.Tools.dll.so" not found 03-17 18:20:43.025 D/Mono ( 4485): AOT module '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/lib/mono/aot-cache/x86/System.Diagnostics.Tools.dll.so' not found: dlopen failed: library "/data/app/App1.Droid-1/lib/x86/libaot-System.Diagnostics.Tools.dll.so" not found 03-17 18:20:43.025 D/Mono ( 4485): Config attempting to parse: 'System.Diagnostics.Tools.dll.config'. 03-17 18:20:43.025 D/Mono ( 4485): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Diagnostics.Tools/System.Diagnostics.Tools.config'. 03-17 18:20:43.025 D/Mono ( 4485): Assembly Ref addref App1[0xeb1e9980] -> System.Diagnostics.Tools[0xee38ae20]: 2 03-17 18:20:43.025 D/Mono ( 4485): Assembly Ref addref System.Diagnostics.Tools[0xee38ae20] -> System[0xeb1ea8e0]: 18 Loaded assembly: System.Diagnostics.Tools.dll [External] 03-17 18:20:43.081 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.Animated.Vector.Drawable[0xeb1e9920] -> Xamarin.Android.Support.Vector.Drawable[0xeb1ea280]: 2 03-17 18:20:43.081 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.Design[0xeb1e9b00] -> Xamarin.Android.Support.v4[0xeb1ea100]: 4 03-17 18:20:43.081 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.Design[0xeb1e9b00] -> Xamarin.Android.Support.v7.AppCompat[0xeb1e9fe0]: 3 03-17 18:20:43.081 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.Design[0xeb1e9b00] -> Xamarin.Android.Support.v7.RecyclerView[0xeb1ea220]: 2 03-17 18:20:43.081 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.v7.RecyclerView[0xeb1ea220] -> Xamarin.Android.Support.v4[0xeb1ea100]: 5 03-17 18:20:43.091 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.v7.MediaRouter[0xeb1ea160] -> Xamarin.Android.Support.v4[0xeb1ea100]: 6 03-17 18:20:43.091 D/Mono ( 4485): Assembly Ref addref Xamarin.Android.Support.v7.MediaRouter[0xeb1ea160] -> Xamarin.Android.Support.v7.AppCompat[0xeb1e9fe0]: 4 03-17 18:20:43.143 I/art ( 4485): Starting a blocking GC Explicit 03-17 18:20:43.150 I/art ( 4485): Explicit concurrent mark sweep GC freed 14963(1005KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 1686KB/2MB, paused 365us total 6.432ms 03-17 18:20:43.150 D/Mono ( 4485): GC_TAR_BRIDGE bridges 88 objects 1233 opaque 481 colors 88 colors-bridged 88 colors-visible 88 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.30ms scc-setup 0.03ms gather-xref 0.01ms xref-setup 0.01ms cleanup 0.20ms 03-17 18:20:43.150 D/Mono ( 4485): GC_BRIDGE: Complete, was running for 7.58ms 03-17 18:20:43.150 D/Mono ( 4485): GC_MINOR: (Nursery full) time 43.12ms, stw 43.61ms promoted 1494K major size: 2576K in use: 1911K los size: 1024K in use: 354K 03-17 18:20:43.166 D/Mono ( 4485): Unloading image System.Diagnostics.Debug.dll [0xd3e21200]. 03-17 18:20:43.166 D/Mono ( 4485): Image addref System.Diagnostics.Debug[0xeb1e8060] -> System.Diagnostics.Debug.dll[0xeabb1b00]: 7 03-17 18:20:43.166 D/Mono ( 4485): Config attempting to parse: 'System.Diagnostics.Debug.dll.config'. 03-17 18:20:43.166 D/Mono ( 4485): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Diagnostics.Debug/System.Diagnostics.Debug.config'. 03-17 18:20:43.166 D/Mono ( 4485): Assembly Ref addref Xamarin.Forms.Xaml[0xeb1ea580] -> System.Diagnostics.Debug[0xeb1ea880]: 5 03-17 18:20:43.205 D/Mono ( 4485): Assembly Ref addref Mono.Android[0xeb1ea340] -> System.Net.Http[0xeb1f6ec0]: 3 03-17 18:20:43.279 D/Mono ( 4485): Assembly Ref addref Mono.Android[0xeb1ea340] -> System.Runtime.Serialization[0xeec14ae0]: 4 03-17 18:20:43.418 D/Mono ( 4485): Unloading image System.Threading.Tasks.dll [0xd3e21c00]. 03-17 18:20:43.418 D/Mono ( 4485): Image addref System.Threading.Tasks[0xeb1e8000] -> System.Threading.Tasks.dll[0xdaa06100]: 6 03-17 18:20:43.418 D/Mono ( 4485): Config attempting to parse: 'System.Threading.Tasks.dll.config'. 03-17 18:20:43.418 D/Mono ( 4485): Config attempting to parse: '/Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Threading.Tasks/System.Threading.Tasks.config'. 03-17 18:20:43.418 D/Mono ( 4485): Assembly Ref addref Xamarin.Interactive[0xeb1f5300] -> System.Threading.Tasks[0xeb1f5f00]: 4 03-17 18:20:43.419 D/Mono ( 4485): Assembly Ref addref Xamarin.Interactive.Android[0xeb1f53c0] -> System[0xeb1ea8e0]: 19 03-17 18:20:43.474 D/Mono ( 4485): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 88 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.30ms scc-setup 0.03ms gather-xref 0.01ms xref-setup 0.01ms cleanup 0.01ms 03-17 18:20:43.474 D/Mono ( 4485): GC_BRIDGE: Complete, was running for 0.10ms 03-17 18:20:43.474 D/Mono ( 4485): GC_MINOR: (Nursery full) time 7.33ms, stw 7.58ms promoted 1984K major size: 5008K in use: 4298K los size: 2048K in use: 1044K 03-17 18:20:43.481 D/Mono ( 4485): Assembly Ref addref System.Text.RegularExpressions[0xeb1f6ce0] -> mscorlib[0xeb1e98c0]: 49 03-17 18:20:43.484 D/Mono ( 4485): Assembly Ref addref System.Net.Primitives[0xd5caa600] -> mscorlib[0xeb1e98c0]: 50 03-17 18:20:43.484 D/Mono ( 4485): Assembly Ref addref System.ComponentModel.DataAnnotations[0xd5cab0e0] -> System[0xeb1ea8e0]: 20 03-17 18:20:43.486 D/Mono ( 4485): Assembly Ref addref System.Xml.XDocument[0xd5cab1a0] -> mscorlib[0xeb1e98c0]: 51 03-17 18:20:43.486 D/Mono ( 4485): Assembly Ref addref System.Xml.Linq[0xd5cab140] -> System[0xeb1ea8e0]: 21 03-17 18:20:43.486 D/Mono ( 4485): Assembly Ref addref System.Xml.Linq[0xd5cab140] -> System.Runtime.Serialization[0xeec14ae0]: 5 InspectorDebugSession(12): HandleTargetEvent: UnhandledException An unhandled exception occured.

0 个答案:

没有答案