【问题标题】:Firebase admin sdk with java doesn't connect带有 java 的 Firebase admin sdk 无法连接
【发布时间】:2019-12-27 13:50:10
【问题描述】:

我正在尝试使用 firebase 数据库来创建 java 桌面 RMI 数据管理系统。我的第一步是使用 admin sdk 确认 firebase 的连接性。我完全按照指南中的步骤进行操作,但没有任何进展。程序执行,但我的 firebase 控制台中没有数据修改。

以下是我到目前为止的代码..

public class Main{

    public static class User2 {

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

        public User2(String dateOfBirth, String fullName) {
            this.date_of_birth = dateOfBirth;
            this.full_name = fullName;
        }

        public User2(String dateOfBirth, String fullName, String nickname) {
            this.date_of_birth = dateOfBirth;
            this.full_name = fullName;
            this.nickname = nickname;
        }
    }

    public static void main(String[] args) {

        FileInputStream serviceAccount = null;
        try {

            serviceAccount = new FileInputStream("rathnapuralabs-firebase-adminsdk-okzic-f6313557b4.json");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        FirebaseOptions options = null;
        try {

            options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("https://rathnapuralabs.firebaseio.com")
                    .build();
        } catch (IOException e) {

            e.printStackTrace();
        }
        FirebaseApp.initializeApp(options);

        DatabaseReference ref = FirebaseDatabase.getInstance()
                .getReference();

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

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

        usersRef.setValueAsync(users);

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {
               Object document = dataSnapshot.getValue();
               System.out.println(document);
           }

           @Override
           public void onCancelled(DatabaseError error) {
           }
    });
    }

这段代码给了我三个 3 行的错误。

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。

SLF4J:默认为无操作 (NOP) 记录器实现

SLF4J:请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder 了解更多详情。

但有件事告诉我,即使出现这些错误,数据仍应保存到 firebase。

以下是代码和密钥 json 的 github 链接。

https://github.com/bandarawaththa/Testing-Firebase-with-realtime-db.git

【问题讨论】:

  • 错误是sl4j logger配置不当,与实际操作无关,暂时可以忽略
  • @Kushan 我也是这么想的……谢谢

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


【解决方案1】:

以下代码对我有用:

try {
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials
                                    .fromStream(new ClassPathResource("/firebase-authentication.json").getInputStream()))
                    .build();

            if (FirebaseApp.getApps().isEmpty()) {
                FirebaseApp.initializeApp(options);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

“/firebase-authentication.json”在资源文件夹中。

【讨论】:

  • 我试过了,还是不行。这些代码非常相似..所以我认为它们都应该可以工作..我的代码也来自firebase指南..你能亲自看看github上的项目吗,如果你这样做的话,也许你可以给我看看你的项目没关系。
  • 我只使用我的项目来测试 Firebase 身份验证。不知道对你有没有用。但是您使用自己的 firebase-authentication 文件对吗?
  • 你说的是json文件吧。是的,我有它
  • 您的 pom.xml 文件在 github 上丢失。你能提供吗?
  • 我没有这样的文件..我需要创建它吗
猜你喜欢
  • 2021-10-16
  • 2017-10-31
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-24
  • 2018-03-20
  • 2020-01-17
相关资源
最近更新 更多