引起:com.fasterxml.jackson.databind.JsonMappingException:无法从String值实例化类型[simple type,class net.we4x4.we4x4.userInformation]的值;没有单字符串构造函数/工厂方法
此错误令我感到困惑,我试图按照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,我仍然是同样的错误,我创建了一个单独的粗心来处理从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社区的好人所建议的那样,我不需要一个二传手,但是当我不在这个类中放置一个二传手时我得到了错误,我只是避免得到这个类的构造错误,只提取一个字符串,当我添加更多字符串时,我仍然得到错误,所以当我添加:
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();
}
});
}
我为这篇长篇文章道歉,但我想提供尽可能多的信息,以便在这里得到一些帮助来理解这些混乱。
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$2.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$1.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$2.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$1.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)
答案 0 :(得分:2)
您正在阅读单个用户对象,因此它没有孩子:
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循环将对用户的{strong>子属性进行迭代,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);
}