这是我目前的代码,但它不起作用。
我正在尝试使用Firebase实时数据库检索单个字符串值(DATA)而不监听更改
但每当我运行此代码时,我都会将电子邮件[0]视为空。
此外,Log.e("Email#1", "hello");
永远不会弹出我的logcat,让我感觉它从未真正进入addListenerForSingleValueEvent()
。
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
Firebase.setAndroidContext(this);
final Button login=(Button)findViewById(R.id.loginbtn);
final Firebase ref=new Firebase("//fir-learn-4991c.firebaseio.com/Users/Sriharsha/email");
//assert login != null;
assert login != null;
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login.setText("Loading...");
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dref=database.getReference("email");
final String user=username.getText().toString();
final String[] email = new String[1];
dref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
Message post = dataSnapshot.getValue(Message.class);
Log.e("Email#1","hello yo");
//System.out.println(post);
//Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
email[0]=post.getEmail();
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
这是课程:消息: -
public class Message {
private String email;
public Message(){
}
public void message(String email)
{
this.email=email;
}
public String getEmail()
{
return email;
}
}
这是我的Firebase数据库: Firebase database picture
答案 0 :(得分:0)
您的侦听器设置在引用email
上(如果它不存在,因此不会触发侦听器),而您的电子邮件字符串实际上存储在Users/<username?>/email
。您可能需要相应地更改dref
。
答案 1 :(得分:0)
我解决了。谢谢大家。我只是没有使用字符串数组,并且还使用不同的方法创建它以从firebase获取直接的String值。
Firebase ref=new Firebase("//fir-learn-4991c.firebaseio.com/Users/"+user);
Firebase ref1=ref.child("email");
final String pass=password.getText().toString();
//Log.e("dref",dref.toString());
ref1.addValueEventListener(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String post=dataSnapshot.getValue(String.class);
Log.v("Email#1",post);
authentication(post,pass,user);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Log.e("Email",""+ email[0]);
Log.e("Ref",""+ ref);
}
});
}
public void newActivity(String email, String pass, String user)
{
Intent intent = new Intent(Login.this, THSTheApp.class);
intent.putExtra("email",email);
intent.putExtra("UserName", user);
intent.putExtra("Password", pass);// pass your values and retrieve them in the other Activity using keyName
startActivity(intent);
}
public void x()
{
Toast.makeText(Login.this, "There Was A Problem",
Toast.LENGTH_SHORT).show();
username.setText("");
password.setText("");
}
public void authentication(final String email1, final String pass,final String user)
{
mAuth = FirebaseAuth.getInstance();
mAuth.signInWithEmailAndPassword(email1, pass).addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
newActivity(email1,pass,user);
}
else if (!task.isSuccessful()) {
x();
}
}
});
}
}