Firebase数据库有困难。无效的数据库路径

时间:2018-06-28 20:59:09

标签: android firebase firebase-realtime-database

struct db由以下原因引起:com.google.firebase.database.DatabaseException:无效的Firebase数据库路径:https://eleicoes-f6d73.firebaseio.com/。 Firebase数据库路径中不得包含“。”,“#”,“ $”,“ [”或“]”  在br.com.eleicoes.eleicoes.MainActivity。(MainActivity.java:37) 06-28 20:16:09.973 28933-28933 / br.com.eleicoes.eleicoes E / AndroidRuntime:致命异常:main

public class MainActivity extends AppCompatActivity {
TextView resposta;
Button bt1;
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("https://eleicoes-f6d73.firebaseio.com/");

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mAuth = FirebaseAuth.getInstance();
    // [START initialize_auth]
    mAuth = FirebaseAuth.getInstance();
    // [END initialize_auth]
}

// [START on_start_check_user]
@Override
public void onStart() {
    super.onStart();
    resposta = (TextView) findViewById(R.id.resposta);
    bt1 = (Button) findViewById(R.id.bt1);
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();


    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String post = dataSnapshot.getValue(String.class);

            resposta.setText(post);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}


// [END on_start_check_user]

private void signInAnonymously() {

    // [START signin_anonymously]
    mAuth.signInAnonymously()
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information

                        FirebaseUser user = mAuth.getCurrentUser();

                    } else {
                        // If sign in fails, display a message to the user.

                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }


                }
            });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


}

error log

3 个答案:

答案 0 :(得分:1)

您不能放入数据库https://eleicoes-f6d73.firebaseio.com/的节点中 那是错误。

答案 1 :(得分:0)

更改

  DatabaseReference reference = database.getReference("https://eleicoesf6d73.firebaseio.com/");

  DatabaseReference reference = database.getReference();

答案 2 :(得分:0)

正如Dan所建议的,没错,您正在引用数据库的主树节点,但是在您的ref.addValueEventListener中,您正试图从无子获得该帖子

例如,如果您想获得第一个,则可以这样做

首先按照您的意愿修正您的引用

来自

 DatabaseReference reference = database.getReference("https://eleicoesf6d73.firebaseio.com/");

DatabaseReference reference = database.getReference();

然后在您的引用中仅调用您想要获取哪个值的孩子,例如lula的值“ 1”

ref.child("lula").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String post = dataSnapshot.getValue(String.class);

            resposta.setText(post);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

如果要获取lula2值,可以从ref.child("lula")...更改为ref.child("lula2")...

编辑:

由于lula不是数据库中的字符串,因此只需更改为

 ref.child("lula").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int post = dataSnapshot.getValue(Integer.class);

                resposta.setText(String.valueOf(post));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });