我正在尝试从 Java Android 项目的 Firebase 实时数据库中读取数据。我正在使用 Android Studio 4.2.2。
我可以很好地获取 JSON 字符串。当我尝试反序列化数据时,问题就开始了。
那时我得到一个例外:“没有定义无参数构造函数。如果您使用 ProGuard,请确保这些构造函数没有被剥离。”
关键是我已经定义了一个无参数构造函数。我还检查了这里的文档https://firebase.google.com/docs/database/android/read-and-write
我的问题是:
- 我做错了什么?
- 如何解析具有多个级别的 JSON?
- 变量的名称是否必须与 JSON 的键名称相同?
提前致谢!
JSON 看起来像这样:
在 Logcat 中,我可以看到 JSON 安全到达应用程序:
{ key = 1, value = {phone=123456789, name=Mike} }
这里有代码:
public class DataBaseHelper {
private DatabaseReference mDatabase;
public TestData result;
public DataBaseHelper() {
mDatabase = FirebaseDatabase.getInstance().getReference();
// Creamos query
Query query = mDatabase.child("TestData");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
try {
Log.w("datosdb", userSnapshot.toString());
result = userSnapshot.getValue(TestData.class);
} catch (Exception e) {
Log.w("datosdb", "Parse failed", e);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w("datosdb", "loadPost:onCancelled", databaseError.toException());
// ...
}
});
}
public class TestData {
public TestData() {
}
public String name;
public int phone;
public String getName() {
return this.name;
}
public int getPhone() {
return this.phone;
}
}
}
这是一个例外:
2021-07-12 00:58:01.631 25262-25262/com.example.purinasa W/datosdb: DataSnapshot { key = 1, value = {phone=123456789, name=Mike} } 2021-07-12 00:58: 01.637 25262-25262/com.example.purinasa W/datosdb:解析失败 com.google.firebase.database.DatabaseException:com.example.purinasa.application.dataBase.DataBaseHelper$TestData 类未定义无参数构造函数。如果您使用 ProGuard,请确保未剥离这些构造函数。在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:570) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper. java:563) 在 com.google.firebase.database.core.utilities 的 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)。
更新:
在使用@Ticherhaz-FreePalestine 进行一些测试后,我们设法使用以下行单独反序列化数据:
String name = userSnapshot.child("name").getValue(String.class);
Log.w("datosdb", "String is here!!!" + name);
long phone = userSnapshot.child("phone").getValue(Long.class);
Log.w("datosdb", "Long is here!!!" + phone);
顺便说一句,我找不到在不引发异常的情况下使用另一个代码的方法。