使用Java的Firebase管理员SDK,解析失败

时间:2018-07-08 05:53:53

标签: java firebase firebase-realtime-database firebase-admin

我是Firebase开发的新手,我试图将一个简单的字符串推送到Firebase实时数据库,但始终会出错。

我的代码

foo=somethingelsenotbar

错误堆栈跟踪:

public static void main(String[] args) {
    try {
        initializeFirebase();
    } catch (IOException e) {
        e.printStackTrace();
    }

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRootRef = database.getReference();
    DatabaseReference condRef = myRootRef.child("condition");

    condRef.setValueAsync("ok", new DatabaseReference.CompletionListener() {
        public void onComplete(DatabaseError arg0, DatabaseReference arg1) {
            System.out.println("success");
        }
    });

}

private static void initializeFirebase() throws IOException {
    FileInputStream serviceAccount = new FileInputStream("adarofirebase.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://adarofirebase.firebaseio.com/").build();

    FirebaseApp.initializeApp(options);
}

我整天都在想办法,但还是没有运气。预先感谢

1 个答案:

答案 0 :(得分:1)

您正在使用错误的参数调用setValueAsync()方法:

public ApiFuture<Void> setValueAsync(Object value, Object priority)

第二个参数是priority,它应该是JSON可解析的值。您正在传递CompletionListener,它不是JSON可解析的。