我正在尝试在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
答案 0 :(得分:4)
我认为问题在于,与持续运行等待事件和回调触发的Node.js不同,Java在到达main方法结束时退出。你想要让主线程退出。这样的事情应该使主线程不会终止。
while(true) {
Thread.Sleep(10000); //Sleep 10 seconds
}
答案 1 :(得分:2)
Firebase工程师在这里,您可以启用日志记录并发布输出吗?您可以通过执行以下操作启用日志记录:
Firebase.getDefaultConfig().setLogLevel(Logger.Level.DEBUG);
另外,您可以为
添加观察者吗?dataRef.child(".info/connected");
并记录您从中获得的事件?这将告诉您何时连接或与Firebase断开连接。
编辑: 这是通过电子邮件解决的。这是客户端库中的一个错误,现在已经解决了。您可以从网站下载最新版本:https://www.firebase.com/docs/downloads.html