我有一个类来从我的数据库中读取数据,但它总是返回null
。
这是java文件
public class UserActivity extends AppCompatActivity {
TextView textView;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private String userID;
private String TAG = "hifiwi";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
textView = (TextView) findViewById(R.id.textView);
//////////////////////////////////////////////////////////////
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Toast.makeText(UserActivity.this, "Signed in with " + user.getEmail(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UserActivity.this, "Successfully signed out...", Toast.LENGTH_SHORT).show();
}
}
};
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
///////////////////////////////////////////////////////////////
}
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
UserInformation uInfo = new UserInformation();
uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail());
uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName());
uInfo.setPassword(ds.child(userID).getValue(UserInformation.class).getPassword());
uInfo.setPhoneno(ds.child(userID).getValue(UserInformation.class).getPhoneno());
Log.i(TAG, " " + uInfo.getEmail()); //Always returning null
Log.i(TAG, " " + uInfo.getName()); //Always returning null
Log.i(TAG, " " + uInfo.getPassword()); //Always returning null
Log.i(TAG, " " + uInfo.getPhoneno()); //Always returning null
}
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
}
}
日志语句始终返回 null 。
这是t2-login
下的数据库结构(项目名称)。
{
"Users" : {
"8dQ8unnqQXa69nkxUFKrcgiTM3S2" : {
"emailuser" : "sam@gmail.com",
"mobileUser" : "1234567899",
"nameuser" : "sam",
"passworduser" : "sammypp"
},
"DIMBAk4CHZdWGbxN9kPESwIrw9b2" : {
"emailuser" : "hsjsjsjjsjsj@mdm.mdmdmdmndn",
"mobileUser" : "9999999999",
"nameuser" : "hshs",
"passworduser" : "ppppppl"
},
"YJhf16ZyfWUn1Dou3BPcIkSSmVm1" : {
"emailuser" : "test2@gmail.com",
"mobileUser" : "1234567892",
"nameuser" : "test2",
"passworduser" : "test2pp"
},
"yQEFEe5x06hvSZP18Mmb6OfNqnB2" : {
"emailuser" : "test1@gmail.com",
"mobileUser" : "1234567891",
"nameuser" : "test1",
"passworduser" : "test1pp"
}
}
}
UserInformation.class
package com.rishav.t2;
/**
* Created by rishav on 6/27/2017.
*/
public class UserInformation {
private String email;
private String name;
private String phoneno;
private String password;
public UserInformation() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
答案 0 :(得分:1)
试试这种方式
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("users");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userList = new ArrayList<User>();
dataSnapshot.getChildrenCount();
//List<User> list= new ArrayList<User>();
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
User user = childDataSnapshot.getValue(User.class);
userList.add(user);
}
Log.e("hello","childDataSnapshot"+ userList.size());
adapter.update(userList);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("MY", "Failed to read value.", error.toException());
}
});
答案 1 :(得分:1)
将myRef设置为:myRef = FirebaseDatabase.getInstance().getReference("Users").child(user.getUid());
同样改变这个:
private void showData(DataSnapshot dataSnapshot) {
UserInformation uInfo = ds.getValue(UserInformation.class);
Log.i(TAG, " " + uInfo.getEmail()); //Always returning null
Log.i(TAG, " " + uInfo.getName()); //Always returning null
Log.i(TAG, " " + uInfo.getPassword()); //Always returning null
Log.i(TAG, " " + uInfo.getPhoneno()); //Always returning null
}
让UserInformation类变量与firebase数据库中存在的键名匹配:
public class UserInformation {
public String emailuser;
public String nameuser;
public String mobileUser;
public String passworduser;
public UserInformation() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
答案 2 :(得分:1)
试试这个
myRef.child("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 3 :(得分:0)
您是否检查了firebase项目设置的规则
Firebase规则提供了一种在执行读写操作时识别用户角色的方法。在执行任何CRUD操作之前,这些规则将在服务器上充当安全层。默认情况下,规则允许用户执行read&amp; amp;只有在认证后才能写入操作。
The below rules allow authenticated users only to read or write data.
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
以下规则允许每个人阅读&amp;无需身份验证即可写入数据。
{
"rules": {
".read": true,
".write": true
}
}
您还可以使用这些规则在插入数据库之前验证数据。例如,下面的规则使用电子邮件正则表达式将名称验证为少于50个字符并使电子邮件有效。
{
"rules": {
".read": true,
".write": true,
"users": {
"$user": {
"name": {
".validate": "newData.isString() && newData.val().length < 50"
},
"email": {
".validate": "newData.isString() && newData.val().matches(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i)"
}
}
}
}
}
有关详细教程,请查看此link