my data

我有类似上面的数据,我试图为此写POJO类,但这使

01-18 21:02:08.101  28838-28838/thamarai.techundefined.com E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: thamarai.techundefined.com, PID: 28838
    com.firebase.client.FirebaseException: Failed to bounce to type
            at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
            at net.simplifiedcoding.imageuploadsample.ProcessImages$2.onDataChange(ProcessImages.java:144)
            at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:53)
            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:5086)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class net.simplifiedcoding.imageuploadsample.ProcessImages$RetriveUserData]: can not instantiate from JSON object (need to add/enable type information?)
            at [Source: java.io.StringReader@41c63110; line: 1, column: 2]
            at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
            at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
            at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
            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:183)
            at net.simplifiedcoding.imageuploadsample.ProcessImages$2.onDataChange(ProcessImages.java:144)
            at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:53)
            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:5086)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)


这是我的代码

ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
               Log.i("FireBase", "snapshot  " + snapshot);
                    RetriveUserData data = snapshot.getValue(RetriveUserData.class);
                    Log.i("FireBase", "Data  " + data.getAPIkey());

            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.i("FireBase", "The read failed: " + firebaseError.getMessage());
            }
});


这是POJO

public class RetriveUserData {
        private String fullName;
        private String apikey;

        public RetriveUserData() {
            // empty default constructor, necessary for Firebase to be able to deserialize blog posts
        }

        public String getFullName() {
            return fullName;
        }

        public String getAPIkey() {
            return apikey;
        }

    }



  更新资料


这是json我正在获取(记录)的内容

DataSnapshot { key = 8c73191e-ba59-417a-b8ae-a5fed541c761, value = {fullName=Thamaraiselvam, apikey=sfsdfsdjfosf} }


看起来我在做小问题,但不知道它在哪里。

最佳答案

从堆栈跟踪中:


  引起原因:com.fasterxml.jackson.databind.JsonMappingException:没有找到类型[简单类型,类net.simplifiedcoding.imageuploadsample.ProcessImages $ RetriveUserData]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息? )


由此看来,您已经将RetriveUserData嵌套在ProcessImages类中。在这种情况下,Java将对外部类的隐式引用添加到RetriveUserData。由于您没有在Firebase中存储该引用,因此它抱怨找不到该引用。

要摆脱此错误消息,请将RetriveUserData标记为静态。这告诉Java不要生成引用:

public static class RetriveUserData {

10-07 22:08