Reputation: 13
I am using Firebase for both authentication and realtime database. My authentication code was successfully run also my enter value to database code also run, but when I am coding for fetch value in database, I am getting run time error trying to enter value at Firebase database:
FATAL EXCEPTION: main
Process:com.xxx.xxx, PID: 22601
com.google.firebase.database.DatabaseException: Invalid Firebase Database
path: https://xxx-exxe.firebaseio.com/. Firebase Database paths must not contain '.', '#', '$', '[', or ']'
My Code is :
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("https://korsa-e03ae.firebaseio.com/");
reference.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
Offerride user = dataSnapshot.getValue(Offerride.class);
if (user == null){
Toast.makeText(getContext(),"User data is null!",Toast.LENGTH_LONG).show();
return;
}
tvsource.setText(user.source + " , " + user.destination + " , " + user.startDate + " , " + user.startTime);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getContext(), "Failefddd", Toast.LENGTH_LONG).show();
}
});
Upvotes: 1
Views: 12360
Reputation: 539
Instead of using getReferance use, getReferanceFromUrl. and in your case: database.getReference.child("posts");
Upvotes: 0
Reputation: 3737
I think the answer is quite obvious you don't need to specific the url
because app is already link to the database
when you set up the project
just change from
DatabaseReference reference = database.getReference("https://korsae03ae.firebaseio.com/");
to
DatabaseReference reference = database.getReference();
Then it should work
Upvotes: 7
Reputation: 2992
Url of your database is in your google-services.json file. By firebase docs https://firebase.google.com/docs/database/admin/retrieve-data to read data, you can do the following:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("posts");
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Post post = dataSnapshot.getValue(Post.class);
System.out.println(post);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
Upvotes: 0