Java 使用Firebase Admin SDK从Firebase实时数据库获取信息
我试图从Firebase实时数据库中获取一些信息,但没有成功。我不知道我做错了什么。我也试过医生的例子,但都不起作用。以下是我的代码和firebase db结构: Topics.java:Java 使用Firebase Admin SDK从Firebase实时数据库获取信息,java,firebase,firebase-realtime-database,Java,Firebase,Firebase Realtime Database,我试图从Firebase实时数据库中获取一些信息,但没有成功。我不知道我做错了什么。我也试过医生的例子,但都不起作用。以下是我的代码和firebase db结构: Topics.java: public class Topics { private String name; public Topics() { } public Topics(String name) { this.name = name; } public String getName() {
public class Topics {
private String name;
public Topics() {
}
public Topics(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Main.java
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream serviceAccount;
FirebaseOptions options = null;
try {
serviceAccount = new FileInputStream(".//...");
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("...")
.build();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
FirebaseApp.initializeApp(options);
String topics = getDatafromFirebase();
System.out.println("Everything right!");
}
private static String getDatafromFirebase() {
CountDownLatch done = new CountDownLatch(1);
StringBuilder b = new StringBuilder();
DatabaseReference dbRef = FirebaseDatabase.getInstance()
.getReference();
dbRef.child("topics").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// TODO Auto-generated method stub
if(snapshot.exists()) {
for(DataSnapshot s:snapshot.getChildren()) {
Topics t = s.getValue(Topics.class);
b.append(t.getName());
b.append(" ");
done.countDown();
}
}
else {
b.append("No existe ");
done.countDown();
}
}
@Override
public void onCancelled(DatabaseError error) {
// TODO Auto-generated method stub
b.append("Error: "+error.getDetails());
done.countDown();
}
});
try {
done.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b.toString();
}
我已经等了
倒计时锁存器
5分钟了,我想这足够让它触发了。另外,重要提示:我已经成功地通过firebase云消息发送了消息,因此我不认为凭据有问题。根据可用文档
在使用Firebase Admin SDK从服务器访问Firebase实时数据库之前,必须使用Firebase对服务器进行身份验证。当您对服务器进行身份验证时,而不是像在客户端应用程序中那样使用用户帐户的凭据登录,而是使用服务帐户进行身份验证,该帐户将您的服务器标识到Firebase
若您并没有在服务器上运行代码,那个么您可以按照描述作为客户端进行身份验证
希望这能有所帮助。我使用相同的数据库结构运行了您的代码,我可以肯定地说,我能够从数据库中获取信息
onDataChange
断点不触发只有在我完全删除主题
子树时才会发生。在你的情况下,一个空的数据库
我怀疑您的数据库url或私钥JSON
按照以下说明获取新私钥
乍一看,代码对我来说很好。如果在
onDataChange
中放置断点,是否会触发该断点?@FrankvanPuffelen在onDataChange
和onCancelled
中放置断点。它们不会被触发。发生这种情况的唯一原因是客户端从未从服务器获得响应。根据我的经验,这可能是因为客户端没有internet连接,或者是因为客户端在数据从服务器返回之前退出。另一个原因可能是凭据设置不正确。在firebase控制台中检查计算机的SHA键是否正确。当你说你使用firebase CloudMessaging时,你的意思是在这段代码中?@Juanje我如何检查firebase和我的计算机中的SHA密钥?不是我的问题中的代码。我编写了一个方法,使用相同的Firebase初始化(相同的凭据、相同的数据库url等)向我的应用程序发送通知。这和从数据库中删除所有项目,然后重新创建使代码工作起来!
package fireb;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class Fireb {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream serviceAccount;
FirebaseOptions options = null;
try {
serviceAccount = new FileInputStream("C:\\key\\testapp-f0fe2-firebase-adminsdk-4po4a-5ce6c60b81.json");
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://testapp-f0fe2.firebaseio.com")
.build();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
FirebaseApp.initializeApp(options);
String topics = getDatafromFirebase();
System.out.println(topics);
System.out.println("Everything right!");
}
private static String getDatafromFirebase() {
CountDownLatch done = new CountDownLatch(1);
StringBuilder b = new StringBuilder();
DatabaseReference dbRef = FirebaseDatabase.getInstance()
.getReference();
dbRef.child("topics").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// TODO Auto-generated method stub
if(snapshot.exists()) {
for(DataSnapshot s:snapshot.getChildren()) {
Topics t = s.getValue(Topics.class);
b.append(t.getName());
b.append(" ");
}
done.countDown();
}
else {
b.append("No existe ");
done.countDown();
}
}
@Override
public void onCancelled(DatabaseError error) {
// TODO Auto-generated method stub
b.append("Error: "+error.getDetails());
done.countDown();
}
});
try {
done.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b.toString();
}
}