我整天都在尝试了解应该从Firebase实时数据库中读写的代码正在发生什么,但是没有成功,并且没有问题,错误,异常或任何可以解决的技巧帮我弄清楚。我使用的是[[针对桌面的java]而不是Android进行编码,因为我在搜索中出现的所有内容都返回了很多,尽管我也使用相同的数据库对Android进行了相同的操作编码,但效果很好。我花了太多时间和精力,而且交期太短,无法将其作为我正在从事的学科的项目,因此,如果有人可以提供帮助,我将不胜感激![在Android中,完全有可能读写数据,我放弃了将问题存入数据库的可能性。我正在按照Firebase Admin page中的说明进行操作。如我所说,没有错误,但也没有结果。使用相同的配置,并且从Firebase控制台获取的json密钥文件可以与Firebase Cloud Messaging一起正常使用,因此我认为密钥也可以。
我正在尝试的代码如下,与Firebase网页几乎相同:
package pt.ipp.estg.housecontrol;
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;
import java.io.FileInputStream;
import java.io.IOException;
class StarterFRD {
public static void main(String[] args) throws IOException {
FileInputStream refreshToken = new FileInputStream("/Users/wdcunha/Development/Java/frdproj/src/main/resources/housecontrolmobile-firebase-adminsdk-qv0hl-f41a07409d.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.setDatabaseUrl("https://housecontrolmobile.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
// As an admin, the app has access to read and write all data, regardless of Security Rules
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users");
System.out.println("ref: "+ref);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object document = dataSnapshot.getValue();
System.out.println("document: "+document);
}
@Override
public void onCancelled(DatabaseError error) {
System.out.println("canceled: "+error.toException());
}
});
Utilizador utilz = new Utilizador("Euquipe", "[email protected]");
System.out.println("utilz nome: "+utilz.getNome());
System.out.println("utilz email: "+utilz.getEmail());
ref.child("2").push().setValueAsync(utilz);
ref.child("3").setValue("I'm writing data", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
System.out.println("Data could not be saved " + databaseError.getMessage());
} else {
System.out.println("Data saved successfully."+databaseReference);
}
}
});
}
}
数据库就是那个:
Link to print of the firebase database I'm using
当我运行代码时,我可以使用println(仅是来自数据库的url)从下面的行中获取数据:
DatabaseReference ref = FirebaseDatabase.getInstance()
它被设置为Maven项目,所以写的pom.xml是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pt.ipp.estg.housecontrol</groupId>
<artifactId>housecontrolserver</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
整个代码在Github here中。
所以,就是这样。即使一整天都在搜索和尝试许多选项之后,我还是一无所知,我真的需要帮助。我希望有人能给我很大的帮助。
我整天都在尝试了解应该从Firebase实时数据库读写的代码正在发生什么,但是没有成功,也没有问题,错误,...]] >> < [