Firebase 不是从字符串值实例化类型的值
Firebase an not instantiate value of type from String value
Caused by: com.fasterxml.jackson.databind.JsonMappingException: 无法从 String 值中实例化 [simple type, class net.we4x4.we4x4.userInformation] 类型的值;没有单字符串 constructor/factory 方法
这个错误让我很困惑,我已经尝试按照 firebase 文档中提供的几个说明进行操作,例如这个:https://www.firebase.com/docs/android/guide/retrieving-data.html
和另一个关于查询数据的教程,这里还有另一个教程:
https://www.simplifiedcoding.net/firebase-android-tutorial-writing-firebase-data/
而且我无法以正确的方式解决问题,我只是想从这个 JASON 获取数据:
{
"data" : {
"ratingPic" : 0,
"ratingVClip" : 36
},
"users" : {
"3c42f867-8c3a-423b-89e8-3fb777ab76f8" : {
"email" : "k@k.com",
"username" : "kkk"
},
"50347968-cedb-4648-8025-f737fea8d4c9" : {
"email" : "a@a.com",
"username" : "aaa"
}
}
}
通过 childEventListner 的 singleValueEventListner,我仍然出现同样的错误,我创建了一个单独的 crass 来处理从 firebase 中提取信息:
public class userInformation {
private String email;
private String username;
public userInformation(){
}
public userInformation(String username){
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
这是令人困惑的部分,正如 firebase 社区的好人所建议的那样,我不需要 setter,但是当我不把 setter 放在这个 class 我得到了错误,我只是避免了使用 class 的这种结构得到错误,它只提取一个字符串,当我添加更多字符串时我仍然得到错误,所以当我添加:
public userInformation(String username, String email){
this.username = username;
this.email = email;
}
我在下面的堆栈跟踪中收到错误?
我的主要方法是:
public void readingData() {
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
userInformation userInformation = postSnapshot.getValue(userInformation.class);
String username = userInformation.getUsername();
String email = userInformation.getEmail();
usernameDisplay.setText(email);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
firebaseError.getMessage();
}
});
}
*** 与 addChildEventListner 产生相同的错误;
public void readingData(){
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
Query queryRef = ref.orderByChild("email");
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String email) {
// Map<String, Objects> extractedEmail = (Map<String, Objects>) dataSnapshot.getValue();
userInformation userInformation = dataSnapshot.getValue(userInformation.class);
email = userInformation.getEmail();
usernameDisplay.setText(email);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChild) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChild) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getApplicationContext(),
"The read failed: " + firebaseError.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
我为冗长的 post 道歉,但我正在尝试提供尽可能多的信息,以便获得一些帮助来理解这里的混乱。
03-26 07:30:42.654 8560-8560/net.we4x4.we4x4 E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.we4x4.we4x4, PID: 8560
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at net.we4x4.we4x4.MainActivity.onDataChange(MainActivity.java:112)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class net.we4x4.we4x4.userInformation] from String value; no single-String constructor/factory method
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
at net.we4x4.we4x4.MainActivity.onDataChange(MainActivity.java:112)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
您正在读取单个用户对象,因此它没有子对象:
public void readingData() {
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
userInformation userInformation = postSnapshot.getValue(userInformation.class);
String username = userInformation.getUsername();
String email = userInformation.getEmail();
usernameDisplay.setText(email);
}
因此 for 循环将迭代用户的 子属性 ,email
和 username
。这些不能反序列化为 userInformation
对象。
解决办法就是不要循环:
public void readingData() {
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userInformation userInformation = dataSnapshot.getValue(userInformation.class);
String username = userInformation.getUsername();
String email = userInformation.getEmail();
usernameDisplay.setText(email);
}
Caused by: com.fasterxml.jackson.databind.JsonMappingException: 无法从 String 值中实例化 [simple type, class net.we4x4.we4x4.userInformation] 类型的值;没有单字符串 constructor/factory 方法
这个错误让我很困惑,我已经尝试按照 firebase 文档中提供的几个说明进行操作,例如这个:https://www.firebase.com/docs/android/guide/retrieving-data.html
和另一个关于查询数据的教程,这里还有另一个教程: https://www.simplifiedcoding.net/firebase-android-tutorial-writing-firebase-data/
而且我无法以正确的方式解决问题,我只是想从这个 JASON 获取数据:
{
"data" : {
"ratingPic" : 0,
"ratingVClip" : 36
},
"users" : {
"3c42f867-8c3a-423b-89e8-3fb777ab76f8" : {
"email" : "k@k.com",
"username" : "kkk"
},
"50347968-cedb-4648-8025-f737fea8d4c9" : {
"email" : "a@a.com",
"username" : "aaa"
}
}
}
通过 childEventListner 的 singleValueEventListner,我仍然出现同样的错误,我创建了一个单独的 crass 来处理从 firebase 中提取信息:
public class userInformation {
private String email;
private String username;
public userInformation(){
}
public userInformation(String username){
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
这是令人困惑的部分,正如 firebase 社区的好人所建议的那样,我不需要 setter,但是当我不把 setter 放在这个 class 我得到了错误,我只是避免了使用 class 的这种结构得到错误,它只提取一个字符串,当我添加更多字符串时我仍然得到错误,所以当我添加:
public userInformation(String username, String email){
this.username = username;
this.email = email;
}
我在下面的堆栈跟踪中收到错误?
我的主要方法是:
public void readingData() {
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
userInformation userInformation = postSnapshot.getValue(userInformation.class);
String username = userInformation.getUsername();
String email = userInformation.getEmail();
usernameDisplay.setText(email);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
firebaseError.getMessage();
}
});
}
*** 与 addChildEventListner 产生相同的错误;
public void readingData(){
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
Query queryRef = ref.orderByChild("email");
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String email) {
// Map<String, Objects> extractedEmail = (Map<String, Objects>) dataSnapshot.getValue();
userInformation userInformation = dataSnapshot.getValue(userInformation.class);
email = userInformation.getEmail();
usernameDisplay.setText(email);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChild) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChild) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getApplicationContext(),
"The read failed: " + firebaseError.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
我为冗长的 post 道歉,但我正在尝试提供尽可能多的信息,以便获得一些帮助来理解这里的混乱。
03-26 07:30:42.654 8560-8560/net.we4x4.we4x4 E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.we4x4.we4x4, PID: 8560
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at net.we4x4.we4x4.MainActivity.onDataChange(MainActivity.java:112)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class net.we4x4.we4x4.userInformation] from String value; no single-String constructor/factory method
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
at net.we4x4.we4x4.MainActivity.onDataChange(MainActivity.java:112)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
您正在读取单个用户对象,因此它没有子对象:
public void readingData() {
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
userInformation userInformation = postSnapshot.getValue(userInformation.class);
String username = userInformation.getUsername();
String email = userInformation.getEmail();
usernameDisplay.setText(email);
}
因此 for 循环将迭代用户的 子属性 ,email
和 username
。这些不能反序列化为 userInformation
对象。
解决办法就是不要循环:
public void readingData() {
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/users/" + UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userInformation userInformation = dataSnapshot.getValue(userInformation.class);
String username = userInformation.getUsername();
String email = userInformation.getEmail();
usernameDisplay.setText(email);
}