Android Firebase-将经过身份验证的用户添加到数据库 [英] Android Firebase - add authenticated user into database

查看:49
本文介绍了Android Firebase-将经过身份验证的用户添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我在Visual Studio中构建xamarin.android应用程序,这种类型的问题很多,但是我真的没有找到任何有关xamarin.android的东西,所有东西都是基于Java的.我尝试按照这些教程和答案进行操作,但是在Xamarin android上总是缺少某些东西或无法正常工作.

Ok, so I am building xamarin.android applicationon in Visual Studio and there is quite a lot of questions of this type, but I really didn't find anything regarding xamarin.android, everything is Java based. I tried following those tutorials and answers but there was always something missing or not working on Xamarin android.

我已经进行了身份验证,它可以很好地工作,那里没有问题.我尝试将用户信息(用户在注册时输入的信息)存储到数据库中.有点奏效,但是有一个问题.这是代码:

I have made authentication and it works great, no problems there. And i tried storing user information (info that user types in while registering) into database. It kind of worked, but with one problem. Here is code:

  public void OnComplete(Task task)
    {
        if (task.IsSuccessful)
        {
            Toast.MakeText(this, "Successful", ToastLength.Long).Show();

            FirebaseUser user = FirebaseAuth.GetInstance(app).CurrentUser;
            id = user.Uid;
            CreateUser();

            buttonSignIn.PerformClick();
            progressBar.Visibility = ViewStates.Invisible;
        }
        else
        {
     //something
        }
    }

这是 CreateUser()方法:

  private void CreateUser ()
    {
        Account user = new Account();
        user.uid = id;
        user.name = signup_inputName;
        user.lastName = signup_inputLastName;
        user.email = signup_inputEmail;
        user.phone = signup_inputPhoneNumber;

        var firebase = new FirebaseClient(FirebaseURL);
        var item = firebase.Child("users").PostAsync<Account>(user);

    }

这是帐户类代码:

public class Account
{
    public string uid      { get; set; }
    public string name     { get; set; }
    public string lastName { get; set; }
    public string email    { get; set; }
    public string phone    { get; set; }

}

此代码将用户信息存储在数据库中"users" 节点下.但是,在"user" 下还有一个带有一些随机值的节点,下面是有关用户的信息(包括 uid ).输出为:

This code stores user information under "users" node in database. But there is one more node under "user" with some random value and below are information about user (including uid). Here is output:

- Users   
      -LBGFtYFTfD3l1hmwHVn
          email:  "testuser@test.com"
          lastName:"peric"
          name:  "pero"
          phone: "12321"
          uid: "18puc5CzSZfzbdflzekzNCHGHR62"

所以,我的问题是,"users" 节点下面的这个随机值不应该是 uid 吗?如果是,该如何设置?我尝试了这个:

So, my question is, shouldn't this random value below "users" node be uid? If yes, how to set it that way? I tried with this:

Account user = new Account();
            user.uid = id;
            user.name = signup_inputName;
            user.lastName = signup_inputLastName;
            user.email = signup_inputEmail;
            user.phone = signup_inputPhoneNumber;

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users").Child(uid).SetValue(user)

但是那没有用,我得到了这个错误:

But that didn't work, I was getting this error:

Firebase No properties to serialize found on class

即使我在用户类中设置了公共获取器和设置器,也是如此.

Even though I have set public getters and setters in users class.

我需要获取该用户信息,并将其显示在我的应用程序中的某些位置,但是我无法使其正常工作.这是我第一次使用Firebase,而且我已经习惯了SQL数据库,因此这确实让我感到困惑.另外,关于xamarin.android和firebase的在线信息很少,而且我实际上对编程还是陌生的.任何帮助将不胜感激.这是我切换到SQL在线数据库之前的最后尝试.

I need to get that user info and show it on some places in my app, but I just can't get it to work. This is my first time using Firebase and I am used to SQL database so this is really confusing to me. Also, there is very little information online about xamarin.android and firebase, plus I am actually new to programming in general. Any help would be appreciate. This is my last try before switching to SQL online database.

推荐答案

调用 PostAsync 时,客户端在调用它的位置下创建一个新的子节点.这类似于HTTP POST 动词以及大多数其他Firebase SDK中的 push()方法.

When you call PostAsync the client creates a new child node under the location that you call it on. This is similar to the HTTP POST verb, and the push() method in most other Firebase SDKs.

要将数据写入您确切指定的位置,请使用 PutAsync :

To write data to a location that you exactly specify, use PutAsync:

var item = firebase.Child("users").Child(uid).PutAsync<Account>(user);

请参阅《 Firebase自述文件》中的示例.Xamarin .

See the example from the Readme of Firebase.Xamarin.

这篇关于Android Firebase-将经过身份验证的用户添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆