我想从Android手机向firebase发送主键,这样我就可以识别谁向我发送数据了。所以基本上我正在制作一个将图像发送到服务器的应用程序。我想查看谁给我发了什么。所以我需要一个主键。任何人都可以帮我解决这个问题吗?就像我如何向firebase服务器发送一些独特的东西?
public void SignUP(View v){
EditText name = (EditText) findViewById(R.id.editTextRegistrationName);
EditText contact = (EditText) findViewById(R.id.editTextRegistrationContact);
EditText city = (EditText) findViewById(R.id.editTextRegistrationCity);
EditText email = (EditText) findViewById(R.id.editTextRegistrationEmail);
EditText pass = (EditText) findViewById(R.id.editTextRegistrationPassword);
String userName = name.getText().toString();
String userContact = contact.getText().toString();
String userCity = city.getText().toString();
String userEmail = email.getText().toString();
String userPassword = pass.getText().toString();
if(isOnline() == true){
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Users").push();
DatabaseReference nam = myRef.child("Name");
nam.setValue(userName);
DatabaseReference contac = myRef.child("Contact");
contac.setValue(userContact);
DatabaseReference cit = myRef.child("City");
cit.setValue(userCity);
DatabaseReference emai = myRef.child("Email");
emai.setValue(userEmail);
DatabaseReference passwor = myRef.child("Password");
passwor.setValue(userPassword);
}
else{
bal.addUser(new UserBean(0,userName,userContact,userCity,userEmail,userPassword));
Toast.makeText(this, "Data Save in DB", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
<强>更新强>
鉴于您希望获得用户的自动生成ID,您只需导入FirebaseAuth,并且只需几行代码就可以为每个用户提供唯一ID。由于数据自动同步,因此无需担心明确将其发送到服务器。您可以在构建JSON树时在代码中随时使用user-id。如果要将其添加到数据中,那么这也是您已经用于其他数据点的简单过程。您在评论中提及的大多数内容已在SDK中正常运行。所以没有必要从头开始重新发明这些功能。
以下代码用于在您的活动中启用Auth。
private FirebaseAuth mAuth;
// ...
mAuth = FirebaseAuth.getInstance();
EmailPasswordActivity.java
文档中的这个简单示例显示了如何使用电子邮件和密码验证某人...
private FirebaseAuth.AuthStateListener mAuthListener;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
// ...
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
EmailPasswordActivity.java
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail:failed", task.getException());
Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed,
Toast.LENGTH_SHORT).show();
}
// ...
}
});
EmailPasswordActivity.java
此示例代码显示如何匿名验证用户,无需任何密码。
private FirebaseAuth.AuthStateListener mAuthListener;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
// ...
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
AnonymousAuthActivity.java
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInAnonymously", task.getException());
Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
AnonymousAuthActivity.java
启用离线数据同步: (Data offline info)
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
如果要强制将数据树的特定分支存储在本地,您还可以引用该位置并调用keepSynced函数传递true。标题中的注释表示:
/ ** *通过在某个位置调用
keepSynced:YES
,将自动下载该位置的数据 *保持同步,即使没有为该位置附加听众。此外,虽然保留了一个位置 *同步,它不会从持久磁盘缓存中逐出。 * * @param keepSynced传递YES以保持此位置同步,传递NO以停止同步。 * /
原始回答
我想您可能想在website上查看Android的示例代码。基本上,我认为push()。getKey()应该返回自动生成的id,如上所述。我对此的看法是,发布行为将自动生成密钥。
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}