基本 Firebase Java 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18604009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic Firebase Java connection
提问by Derek
I am trying to make a basic connection to firebase within a Java app, and using the basic code provided I am unable to get any response at all.
我正在尝试在 Java 应用程序中与 firebase 建立基本连接,并使用基本代码,前提是我根本无法获得任何响应。
Here is the code I am using:
这是我正在使用的代码:
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");
}
}
The library seems to be loading fine. Any suggestions?
该库似乎加载良好。有什么建议?
Update
更新
[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
采纳答案by Greg Soltis
Firebase Engineer here, can you enable logging and post the output? You can enable logging by doing:
Firebase 工程师,您可以启用日志记录并发布输出吗?您可以通过执行以下操作启用日志记录:
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.
并记录您从中获得的事件?这会告诉您何时与 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
编辑:这是通过电子邮件解决的。这是客户端库中的一个错误,现在已解决。您可以从网站下载最新版本:https: //www.firebase.com/docs/downloads.html
回答by Retsam
I believe the probelm is that, unlike Node.js which keeps running waiting for events and callbacks to fire, Java quits when it reaches the end of the main method. You'll want to keep the main thread from quitting. Something like this should keep the main thread from terminating.
我相信问题在于,与不断运行等待事件和回调触发的 Node.js 不同,Java 在到达 main 方法的末尾时退出。你会想要保持主线程不退出。这样的事情应该可以防止主线程终止。
while(true) {
Thread.Sleep(10000); //Sleep 10 seconds
}