7

我正在尝试在 Java 应用程序中建立与 firebase 的基本连接,并且使用提供的基本代码我根本无法得到任何响应。

这是我正在使用的代码:

package fix;

import com.firebase.client.*;

public class Main {

  public static void main(String args[]) throws Exception {

    String url = "https://----.firebaseIO.com/";
    Firebase dataRef = new Firebase(url);

    dataRef.addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot snapshot) {
             System.out.println("data");
         }

         @Override
         public void onCancelled() {
             System.err.println("Listener was cancelled");
         }
    });

    System.out.println("hi");
  }
}

该库似乎加载正常。有什么建议么?

更新

[java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Raising events for /.info/authenticated
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Doing onDiff with changes: []
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queries complete for /.info/authenticated
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queries complete for /.info/authenticated
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queryMap complete:null
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queries complete for /.info/authenticated
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queryMap complete:null
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Raising events for /.info/connected
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Doing onDiff with changes: []
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queries complete for /.info/connected
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queries complete for /.info/connected
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queryMap complete:null
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queries complete for /.info/connected
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Marking queryMap complete:null
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] ViewManager: Adding Value Event Listener callback for path: /
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] PersistentConnection: pc_0 - Listening on / for [{}]
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] PersistentConnection: pc_0 - Adding listen params: [{}]
 [java] Fri Sep 06 09:54:43 PDT 2013 [DEBUG] EventRaiser: Raising 0 event(s)
 [java] wait
 [java] wait
 [java] wait
 [java] wait
4

2 回答 2

4

我相信问题在于,与一直运行等待事件和回调触发的 Node.js 不同,Java 在到达 main 方法的末尾时退出。你会想要阻止主线程退出。这样的事情应该防止主线程终止。

while(true) {
     Thread.Sleep(10000); //Sleep 10 seconds
}
于 2013-09-04T02:22:44.970 回答
2

Firebase Engineer here, can you enable logging and post the output? You can enable logging by doing:

Firebase.getDefaultConfig().setLogLevel(Logger.Level.DEBUG);

Also, can you add an observer for

dataRef.child(".info/connected");

And log the events that you get from it? That will tell you when you are connected or disconnected from Firebase.

Edit: This was resolved over email. It was a bug in the client library, which is now resolved. You can download the latest version from the website: https://www.firebase.com/docs/downloads.html

于 2013-09-06T16:52:02.113 回答