我想将数据保存到firebase。这是我的活动代码:
private Firebase firebaseRef;
Firebase.setAndroidContext(this);
try {
mUserId = firebaseRef.getAuth().getUid();
} catch (Exception e) {
// loginWithMailView();
}
itemsUrl = Constants.FIREBASE_URL + "/Person/" + mUserId +"/Information/";
//Person + null (?) + the other information
这是我的按钮代码:
buttonSave = (Button) findViewById(R.id.btnKaydolKayit);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firebaseRef = new Firebase(Constants.FIREBASE_URL);
String name = editTextName.getText().toString().trim();
String surname = editTextSurname.getText().toString().trim();
String gender = final_result.getText().toString();
String mail = editTextMail.getText().toString();
String pass = editTextPass.getText().toString().trim();
String phone = editTextPhone.getText().toString().trim();
String trom = final_resultT.getText().toString();
String bloodGroup = spinnerBlood.getSelectedItem().toString();
String dateBlood = dateTextViewBlood.getText().toString();
String dateTrombo = dateTextViewTrom.getText().toString();
if (pass.isEmpty() || mail.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
firebaseRef.createUser(mail, pass, new Firebase.ResultHandler(){
@Override
public void onSuccess() {
firebaseRef = new Firebase(Constants.FIREBASE_URL);
// firebaseRef.setValue();
AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
builder.setMessage("Hesap başarılı bir şekilde oluşturuldu!").setPositiveButton(R.string.login_button_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(KaydolActivity.this, loginWithMail.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
@Override
public void onError(FirebaseError firebaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(KaydolActivity.this);
builder.setMessage(firebaseError.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
new Firebase(itemsUrl).push().child("name").setValue(name);
new Firebase(itemsUrl).push().child("surname").setValue(surname);
new Firebase(itemsUrl).push().child("gender").setValue(gender);
new Firebase(itemsUrl).push().child("mail").setValue(mail);
new Firebase(itemsUrl).push().child("pass").setValue(pass);
new Firebase(itemsUrl).push().child("phone").setValue(phone);
new Firebase(itemsUrl).push().child("trom").setValue(trom);
new Firebase(itemsUrl).push().child("bloodGroup").setValue(bloodGroup);
new Firebase(itemsUrl).push().child("trom").setValue(trom);
new Firebase(itemsUrl).push().child("dateBlood").setValue(dateBlood);
new Firebase(itemsUrl).push().child("dateTrombo").setValue(dateTrombo);
}
});
我的源代码(例如求助):https://www.sitepoint.com/creating-a-cloud-backend-for-your-android-app-using-firebase/
答案 0 :(得分:20)
您可以使用这些代码行通过Android Firebase Framework获取当前用户Uid
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
Toast.makeText(this, "" + currentFirebaseUser.getUid(), Toast.LENGTH_SHORT).show();
答案 1 :(得分:3)
问题是,在创建用户时,在firebase中,它不会签署用户。这是来自文档:
使用提供的凭据创建新的电子邮件/密码用户。此方法不会对用户进行身份验证。创建帐户后,可以使用authWithPassword()对用户进行身份验证。 https://www.firebase.com/docs/web/api/firebase/createuser.html
Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithPassword(mail, pass, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
// there was an error
}
});
因此,一旦创建了用户,请调用authWithPassword函数。希望它有所帮助!
答案 2 :(得分:2)
要获取当前用户UID,您只需要以下代码:
String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();
答案 3 :(得分:0)
我在获取uid时也遇到了同样的问题。 当我将其打印在日志(Log.i)上时,我可以看到uid,但是,当尝试将其用作要创建的对象之一的字段之一时,不会创建该字段。
我正在做的事情:字符串currentUId = FirebaseAuth.getInstance()。getCurrentUser()。getUid();
这对我来说效果不好。
我发现的解决方案-(为我工作-试试吧)
使用:FirebaseUser currentU = FirebaseAuth.getInstance()。getCurrentUser(); 字符串uId = currentU.getUid();
答案 4 :(得分:0)
在REACT NATIVE中就是这样。
import firebase as 'firebase';
HandleGetUserId=()=>{
let userId = firebase.auth().currentUser.uid;
return userId;
}
只需调用函数HandleGetUserId即可获取ID
this.HandleGetUserId();
答案 5 :(得分:0)
在您的代码中,您的firebaseRef似乎为空,因此结果为空uid
,从Firebase获取用户ID的推荐方法是在用户登录后在应用程序的任何位置进行操作
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();