【问题标题】:Calling the PostAsync method crashes the application调用 PostAsync 方法会使应用程序崩溃
【发布时间】:2019-05-15 13:24:00
【问题描述】:

我正在尝试使用 post 方法将用户信息发送到服务器。当我调用异步任务时,它立即崩溃。

这是我在 LoginPage.cs 中的内容

 async void SignInProcedure(object sender, EventArgs e) //function called when pressing the signin button
        {

            User user = new User(Entry_Username.Text, Entry_Password.Text); //putting username and password entered by the user in the entry box
            if (user.CheckInformation())//checks if the boxes are empty
            {

                var result = await App.RestService.Login(user);//RestService class
                await App.Current.MainPage.DisplayAlert("Login", "Login Successful", "Oke");

在 RestService.cs 中

public async Task<Token> Login(User user)//this is where the problem is
        {

            var postData = new List<KeyValuePair<string, string>>();            
            postData.Add(new KeyValuePair<string, string>("grant_type", grant_type));
            postData.Add(new KeyValuePair<string, string>("username", user.Username));
            postData.Add(new KeyValuePair<string, string>("password", user.Password));
            var content = new FormUrlEncodedContent(postData);
            var response = await PostResponseLogin<Token>(Constants.LoginUrl, content);
            DateTime dt = new DateTime();
            dt = DateTime.Today;
            response.expire_date = dt.AddSeconds(response.expire_in);
            return response;

        }

        public async Task<T> PostResponseLogin<T>(string weburl, FormUrlEncodedContent content) where T : class
        {

            var response = await client.PostAsync(weburl,content);

            var jsonResult = response.Content.ReadAsStringAsync().Result;

            var responseObject = JsonConvert.DeserializeObject<T>(jsonResult);

            return responseObject;
        }

服务器应该收到用户数据,但它没有收到任何东西。 我似乎找不到任何错误消息。

【问题讨论】:

  • 您是否尝试过捕获异常?
  • 如果PostAsync 抛出异常,查看异常详细信息将非常有帮助。

标签: c# xamarin xamarin.forms dotnet-httpclient


【解决方案1】:

使用 SignInProcedure 而不是使用 async void,使用 async Task 并添加 try catch 块并检查。

【讨论】:

    【解决方案2】:

    我稍微修改了我的代码

    async Task SignInProcedure(object sender, EventArgs e) //function called when pressing the signin button
            {
    
                User user = new User(Entry_Username.Text, Entry_Password.Text); //putting username and password entered by the user in the entry box
                if (user.CheckInformation())//checks if the boxes are empty
                {
                    try
                    {
                        var result = await App.RestService.Login(user);//RestService class
                        await App.Current.MainPage.DisplayAlert("Login", "Login Successful", "Oke");
                        if (result.access_token != null)
                        {
                            App.UserDatabase.SaveUser(user);
                        }
                    }
                    catch (Exception ex)
                    {
    
                        System.Diagnostics.Debug.WriteLine(ex);
                    }
    

    将 void 更改为 Task 时出现此错误:Position 12:54. Signature (return type) of EventHandler "SmartLockApp.Views.LoginPage.SignInProcedure" doesn't match the event type

    而且,如果我不将 void 更改为 Task,我无法在控制台中找到错误

    【讨论】:

    • 这是我的错,我们不能将 void 更改为 Task for event。只需重置为无效。
    • 您是否真的解决了您的问题,或者这只是附加信息?如果是后者,请编辑您的问题或发表评论。不要仅仅为了添加更多信息而使用答案。
    • 没关系,别担心,我改了而且我还添加了其他信息,我会编辑我的答案
    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多