相关疑难解决方法(0)

使用Java从firebase获取数据

我在下面的代码中遇到了一个问题,几乎逐字逐句从Firebase SDK Java文档中复制了.我是一个真正的语言,比如来自PHP和JavaScript的webdev背景的Java.

基本上,addListenerForSingleValueEvent没有触发来给我回数据.我注意到了这一点,因为系统打印输出没有触发,因此我认为监听事件没有触发.

我怀疑这与我有限的知识如何与函数本身的工作方式有关,就像我缺少一些关于类和函数如何相互作用的结构知识.

任何帮助将不胜感激,谢谢.

class FireBase {
public static void main(String[] args) throws IOException {
    // Fetch the service account key JSON file contents
    FileInputStream serviceAccount = new FileInputStream("key.json");

    // Initialize the app with a service account, granting admin privileges
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
        .setDatabaseUrl("https://ssworks-adwords.firebaseio.com/")
        .build();
    FirebaseApp.initializeApp(options);

    // As an admin, the app has access to read and write all data, regardless of Security Rules
    DatabaseReference ref = FirebaseDatabase
        .getInstance()
        .getReference("petitions");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public …
Run Code Online (Sandbox Code Playgroud)

java firebase firebase-realtime-database

1
推荐指数
1
解决办法
844
查看次数