关于反序列化的Gson异常(无参数构造函数不存在) [英] Gson Exception on deserialize (no-args constructor does not exist)

查看:791
本文介绍了关于反序列化的Gson异常(无参数构造函数不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题只发生在5000个设备中的10个。我的模拟器和测试设备不可能重现。这似乎是一个非常具体的问题,只有少数设备。我得到的只是堆栈跟踪和我的代码。所以我正在开发一个黑洞,只有在GooglePlay发布新版本之后,我才看到这些更改是否解决了问题。我刚刚向GooglePlay更新了一个新版本,但错误仍然存​​在。



这些变化是无参数构造函数,并且改变了WorkIem

以下是堆栈跟踪:

  W 6758 / dalvikvm:threadid = 1:线程以未捕获的异常退出(group = 0x401a15a0)
E 6758 / AndroidRuntime:致命异常:main
**由...引起:java.lang.RuntimeException:类com.mypackage.model.UpdaterObject的No-args构造函数$ UpdateReport不存在。用Gson注册一个InstanceCreator来解决这个问题。**
在com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
在com.google.gson.MappedObjectConstructor.construct (MappedObjectConstructor.java:53)
at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:40)
at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
在com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
在com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
在com.google。 gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
在com.google.gson.Gson.fromJson(Gson.java:568)
在com.google.gson.Gson.fromJson(Gson。 java:515)
在com.google.gson.Gson.fromJson(Gson.java:484)
a t com.google.gson.Gson.fromJson(Gson.java:434)
在com.google.gson.Gson.fromJson(Gson.java:406)
** at de.fliese.NewVersionCheckerActivity .onCreate(NewVersionCheckerActivity.java:35)**

这是第35行:

  UpdateReport report = new Gson()。fromJson(jsonStr,UpdateReport.class); 

json-String看起来像这样(Workitems可以为null):

  {WorkItems:
[{Url:http://url1.comfoo.zip,TypeId LastModifiedServer:1352598239000},
{Url:http://url2.com/bar.zip,TypeId:4,LastModifiedServer:1352598265000}],
ShowQuestionDialog :false,IsOffline:false,DoUpdate:true}

最后,这是我的带有innerClass的UpdateReport类:

  public class UpdaterObject {
public class UpdateReport {

public boolean IsOffline;
public boolean DoUpdate;
public boolean ShowQuestionDialog;
public List< WorkItem>工作项;

public UpdateReport(){}

public UpdateReport(boolean isoffline,boolean doUpdate,
List< WorkItem> workitems){
IsOffline = isoffline;
DoUpdate = doUpdate;
WorkItems =工作项;
}
}

public static class WorkItem {

public int TypeId;
public String Url;
public long LastModifiedServer;

public WorkItem(){}
$ b public WorkItem(int typeId,String url,long lastModifiedServer){
TypeId = typeId;
Url = url;
LastModifiedServer = lastModifiedServer;
}
}
//某些方法[...]
} // end class UpdaterObject

如果有人能帮助我,我会很高兴。请确保并确保您的答案绝对正确,因为我无法测试它!那太好了!

这是我在StackOverFlow上的第一篇文章。请公平。 :)



感谢qd0r

解决方案

From Gson issue 255 ,HTC Desire HD在其中包含一份陈旧的Gson副本应用程序类路径。该问题有关于使用jarjar重新包装您的Gson版本以免与预装版本冲突的说明。


I've got a problem which occurs only in 10 of 5000 devices. There is no possibility to reproduce it with my emulators and test devices. It seems to be a very specific problem with only a few devices. All I've got is the stacktrace and my code. So I'm developing against a black hole and only after I released the new version at GooglePlay I see if the changes solved the problem. I just updated a new version to GooglePlay, but the error still exists.

The changes were parameterless contructors and change the WorkIem class to static.

Here is the stacktrace:

W 6758/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x401a15a0)
E 6758/AndroidRuntime: FATAL EXCEPTION: main
**Caused by: java.lang.RuntimeException: No-args constructor for class com.mypackage.model.UpdaterObject$UpdateReport does not exist. Register an InstanceCreator with Gson for this type to fix this problem.**
    at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
    at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
    at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:40)
    at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
    at com.google.gson.Gson.fromJson(Gson.java:568)
    at com.google.gson.Gson.fromJson(Gson.java:515)
    at com.google.gson.Gson.fromJson(Gson.java:484)
    at com.google.gson.Gson.fromJson(Gson.java:434)
    at com.google.gson.Gson.fromJson(Gson.java:406)
    **at de.fliese.NewVersionCheckerActivity.onCreate(NewVersionCheckerActivity.java:35)**

This is line 35:

UpdateReport report = new Gson().fromJson(jsonStr, UpdateReport.class);

The json-String looks like this (Workitems can be null):

{"WorkItems":
[{"Url":"http://url1.comfoo.zip","TypeId":1,"LastModifiedServer":1352598239000},
{"Url":"http://url2.com/bar.zip","TypeId":4,"LastModifiedServer":1352598265000}],
 "ShowQuestionDialog":false,"IsOffline":false,"DoUpdate":true}

And finally here is my UpdateReport class with the innerClass:

public class UpdaterObject{     
public class UpdateReport {

            public boolean IsOffline;
            public boolean DoUpdate;
            public boolean ShowQuestionDialog;
            public List<WorkItem> WorkItems;

            public UpdateReport() {        }

            public UpdateReport(boolean isoffline, boolean doUpdate,
                    List<WorkItem> workitems) {
                IsOffline = isoffline;
                DoUpdate = doUpdate;
                WorkItems = workitems;
            }
        }

        public static class WorkItem {

            public int TypeId;
            public String Url;
            public long LastModifiedServer;

            public WorkItem() {        }

            public WorkItem(int typeId, String url, long lastModifiedServer) {
                TypeId = typeId;
                Url = url;
                LastModifiedServer = lastModifiedServer;
            }
        }
    //some methods [....]
    } //end class UpdaterObject

I would be happy, if someone could help me. Please make sure and be sure that your answer ist absolutely correct, because I can't test it! That would be great!

This is my first posting on StackOverFlow. Please be fair. :)

Thanks qd0r

解决方案

From Gson issue 255, The HTC Desire HD included an obsolete copy of Gson in its application classpath. That issue has instructions on using jarjar to repackage your version of Gson as to not conflict with the preinstalled version.

这篇关于关于反序列化的Gson异常(无参数构造函数不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆