【发布时间】:2015-08-02 23:09:57
【问题描述】:
我在我的 android 应用程序中使用 GSON 和 Volley 库进行网络连接,但是在使用 Gson 将 Json 响应转换为 Model 类时,出现以下错误:
88-2006/ E/Volley﹕ [121] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
这些是我正在使用的 POJO 类: 产品.java
public class Product {
private String status;
private List<Result> results = new ArrayList<Result>();
private Pagination pagination;
public Product(){}
// getters and setters
}
分页.java
public class Pagination {
private String first;
private String previous;
private String next;
private String last;
public Pagination(){}
}
结果.java
public class Result {
private String id;
private Properties properties;
public Result{}
}
Properties.java
public class Properties {
private String qbcode;
private String name;
private String purchasedate;
private String vendor;
private String thumbnail;
public Properties(){}
}
我已经解决了类似这样的现有问题,根据我发现的答案,我在所有类中添加了 no arg 构造函数,但我仍然收到错误请帮助我解决这个问题
Json 字符串:
{
"status" : "OK",
"results" : [ {
"id" : "IzIzOjE=",
"properties" : {
"qbcode" : "IN-1-1",
"name" : "Test Name",
"purchasedate" : "2015-05-21",
"vendor" : "Test Vendor",
"thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
}
}, {
"id" : "IzIzOjI=",
"properties" : {
"qbcode" : "IN-1-2",
"name" : "Test Name",
"purchasedate" : "2015-05-21",
"vendor" : "Test Vendor",
"thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
}
}, {
"id" : "IzIzOjM=",
"properties" : {
"qbcode" : "IN-1-3",
"name" : "Test Name",
"purchasedate" : "2015-05-21",
"vendor" : "Test Vendor",
"thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
}
},{
"id" : "IzIzOjU=",
"properties" : {
"qbcode" : "IN-1-5",
"name" : "Test Name",
"purchasedate" : "2015-05-21",
"vendor" : "Test Vendor",
"thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
}
} ],
"pagination" : {
"first" : "/list?size=20",
"previous" : "/list?start=IzIzOjE=&size=20",
"next" : "/list?start=IzIzOjQx&size=20",
"last" : "/list?start=IzIzOjYx&size=20"
}
}
【问题讨论】:
-
请同时包含json数据。
-
你的所有类中都添加了标准构造函数(无 arg 构造函数)吗?
-
你是否公开了无参数构造函数?
-
@SachinGupta 我已经编辑了我的问题