Firebase Java Admin SDK无法正常工作 [英] Firebase Java Admin SDK don't work

查看:99
本文介绍了Firebase Java Admin SDK无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循Firebase网站上的文档来设置Java Admin SDK.因此,我将依赖项添加到build.gradle中,并添加了以下代码:

I am following the documentation on Firebase website on setting up an Java Admin SDK. So I add the dependency to the build.gradle, and added the following code:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Main {

    public static void main(String[] args) throws FileNotFoundException{
        FileInputStream serviceAccount = new FileInputStream("/Users/idanaviv10/Desktop/mapit-33290-firebase-adminsdk-fdy24-a1d0505493.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
          .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
          .setDatabaseUrl("https://mapit-33290.firebaseio.com/")
          .build();

        FirebaseApp.initializeApp(options);
        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference ref = database.getReference("server/saving-data/fireblog");

        DatabaseReference usersRef = ref.child("users");

        Map<String, User> users = new HashMap<String, User>();
        users.put("alanisawesome", new User("June 23, 1912", "Alan Turing"));
        users.put("gracehop", new User("December 9, 1906", "Grace Hopper"));

        usersRef.setValue(users);
    }
    public static class User {

        public String date_of_birth;
        public String full_name;
        public String nickname;

        public User(String date_of_birth, String full_name) {
            // ...
        }

        public User(String date_of_birth, String full_name, String nickname) {
            // ...
        }

    }
}

问题是当我尝试获取数据(添加侦听器)或试图保存数据(如示例中)时,什么都没发生.我正在eclipse中运行代码,我可以在控制台中看到它显示完成",但是当我检查数据库(在浏览器的firebase控制台上)时,什么都没有改变.

The problem is when I am trying to get data (adding a listener) or trying to save data (like in the example) nothing happened. I am running the code in eclipse, I can see in the console that it print "Done", but when I check the database (on the firebase console on the browser) nothing change.

推荐答案

我发现了问题,问题在于该程序将在连接到Firebase服务器之前终止.您需要做的是通过调用Thread.sleep(20000);或像我一样

I found the problem, the problem was that the program will terminate before connecting to the Firebase server. What you need to do is to delay the termination of the program, by either calling Thread.sleep(20000);, or like I did it

Scanner scanner = new Scanner(System.in);
while(!scanner.nextLine().equals("Quit")){}
System.exit(0);  

这篇关于Firebase Java Admin SDK无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆