0

目前,我成功使用Firebase電子郵件身份驗證來創建新用戶,但我想將用戶名,地址等個人詳細信息添加到Firebase數據庫。我完成了從電子郵件身份驗證中使用uid作爲孩子到節點的所有研究,但代碼仍然無法正常工作,即使android studio顯示連接的數據庫也會插入任何內容。那麼任何人都可以請教我如何解決這個問題?Firebase電子郵件身份驗證將用戶名和詳細信息添加到Firebase數據庫中

package com.example.rex.ota20; 

import android.content.Intent; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import com.google.android.gms.tasks.OnCompleteListener; 
import com.google.android.gms.tasks.Task; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.auth.FirebaseUser; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import java.util.HashMap; 
import java.util.Map; 

public class Register extends AppCompatActivity { 

private FirebaseAuth auth; 
private EditText run, rpw,re; 
//private EditText rcfpw, rage, , rpn, ra, rpc; 
//private RadioButton rbm, rbfm, rbstd, rbtt; 
private String usnm, pswd, email, uid; 
private FirebaseDatabase db; 
private DatabaseReference ref; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 

    auth = FirebaseAuth.getInstance(); 
    db = FirebaseDatabase.getInstance(); 
    ref = db.getReference(); 
    FirebaseUser user = auth.getCurrentUser(); 
    uid = user.getUid();//getuser id 

    run = (EditText) findViewById(R.id.Run); 
    rpw = (EditText) findViewById(R.id.Rpw); 
    //rcfpw = (EditText) findViewById(R.id.Rcpw); 
    //rage = (EditText) findViewById(R.id.Rage); 

    //rbm = (RadioButton) findViewById(R.id.rbmale); 
    //rbfm = (RadioButton) findViewById(R.id.rbfemale); 

    re = (EditText) findViewById(R.id.Re); 
    //rpn = (EditText) findViewById(R.id.Rpn); 
    //ra = (EditText) findViewById(R.id.Ra); 
    //rpc = (EditText) findViewById(R.id.Rpc); 

    //rbstd = (RadioButton) findViewById(R.id.rbstd); 
    //rbtt= (RadioButton) findViewById(R.id.rbtt); 

    final Button br = (Button) findViewById(R.id.Rbt); 



    br.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      //if(!TextUtils.isEmpty(usnm) || !TextUtils.isEmpty(pswd) || !TextUtils.isEmpty(email)){ 
      usnm = run.getText().toString().trim(); 
      pswd = rpw.getText().toString().trim(); 
      //confirmpw = rcfpw.getText().toString().trim(); 
      email = re.getText().toString().trim(); 
      register(usnm, pswd, email); 

      //}// end of validate 
     }//end of onclick 
    }); 
} 

private void register (final String un, final String pw, final String em){ 
    Task<AuthResult> authResultTask = auth.createUserWithEmailAndPassword(em, pw).addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
     @Override 
     public void onComplete(@NonNull Task<AuthResult> task) { 

      if (task.isSuccessful()) { 
       DatabaseReference dbref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://otapp-61bef.firebaseio.com/").child("users"); 
       uid = dbref.push().getKey(); 
       Toast.makeText(Register.this, "Success Registed", Toast.LENGTH_LONG).show(); 
       Intent i = new Intent(Register.this, StudentHomePage.class); 
       startActivity(i); 
       finish(); 
      } else { 
       Toast.makeText(Register.this, "Error", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
}// end of signup 

/*private void generateUser (String username, String password, String email){ 
    User u = new User (username, password, email); 
    dbref.child("users").setValue(u); 
}*/ 
} 

和規則是

{ 
"rules": { 
    ".read": true, 
    ".write": true 
    } 
} 

回答

1

工作xample:

用戶類

@IgnoreExtraProperties 
public class User { 

    private String name; 
    private String surname; 
    private String phone; 

    public User() { 

    } 

    public User(String phone) { 
     this.phone = phone; 
    } 

    public User(String name, String surname, String phone) { 
     this.name = name; 
     this.surname = surname; 
     this.phone = phone; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public String getPhone() { 
     return phone; 
    } 
} 

活性的方法

private void writeNewUser(DatabaseReference databaseReference, String id, String name, String surname, String phone) { 
     User user = new User(name, surname, phone); 
     databaseReference.child(id).setValue(user); 
    } 
+0

thankyou ...我發現我的問題...>< –

+0

如果喜歡的照片也可以直接插入到數據庫中嗎? –

+0

您可以將其保存在Firebase存儲上。如果你需要另一個工作的例子,只是讓我知道 –

相關問題