Invalid Firebase Path error while fetching database

家住魔仙堡 提交于 2019-12-24 07:43:58

问题


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();
    }
});

回答1:


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




回答2:


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());
}
});



回答3:


Instead of using getReferance use, getReferanceFromUrl. and in your case: database.getReference.child("posts");



来源:https://stackoverflow.com/questions/43466508/invalid-firebase-path-error-while-fetching-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!