java 无法为 Product.class 调用无参数构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30371096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to invoke no-args constructor for Product.class
提问by Manikanta
I am Using GSON and Volley library for networking in my android application but while converting the Json response to Model classes using Gson i am getitng the following error:
我在我的 android 应用程序中使用 GSON 和 Volley 库进行网络连接,但是在使用 Gson 将 Json 响应转换为模型类时,我得到了以下错误:
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.
these are my POJO classes i am using : Product.java
这些是我正在使用的 POJO 类:Product.java
public class Product {
private String status;
private List<Result> results = new ArrayList<Result>();
private Pagination pagination;
public Product(){}
// getters and setters
}
Pagination.java
分页文件
public class Pagination {
private String first;
private String previous;
private String next;
private String last;
public Pagination(){}
}
Result.java
结果.java
public class Result {
private String id;
private Properties properties;
public Result{}
}
}
Properties.java
属性.java
public class Properties {
private String qbcode;
private String name;
private String purchasedate;
private String vendor;
private String thumbnail;
public Properties(){}
}
I have gone through the existing questions which are same like this , as per answers i have found i added the no arg constructor to all the classes but still i am getting the error please help me in solving this issue
我已经完成了与此相同的现有问题,根据我发现的答案,我向所有类添加了 no arg 构造函数,但仍然出现错误,请帮助我解决此问题
The Json String:
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"
}
}
回答by Flaxie
Add default constructor for all classes. Example:
为所有类添加默认构造函数。例子:
public class Product{
public Product(){
}
}
回答by Manikanta
problem solved, I am doing a foolish call To Gson because i am obtaining a Product class that contains list of other Classes(Result.class etc) but i am sending Product[].class to Gson to convert Hence it thrown exception.
问题解决了,我对 Gson 做了一个愚蠢的调用,因为我正在获取一个包含其他类(Result.class 等)列表的 Product 类,但我将 Product[].class 发送到 Gson 进行转换,因此它抛出了异常。
回答by Neeraj Singh
Your model having private attribute you must create setter for the same or create constructor with all parameter like below:
您的模型具有私有属性,您必须为其创建 setter 或创建具有所有参数的构造函数,如下所示:
public class Product {
private String status;
private List<Result> results = new ArrayList<Result>();
private Pagination pagination;
public void setStatus(String status) {
this.status = status;
}
public void setResults(List<Result> results) {
this.results = results;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
}
or
或者
public class Product {
private String status;
private List<Result> results = new ArrayList<Result>();
private Pagination pagination;
public Product(String status, List<Result> results, Pagination pagination) {
this.status = status;
this.results = results;
this.pagination = pagination;
}
}
回答by Kartheek
Just add an no argument constructor for the Bean class.
只需为 Bean 类添加一个无参数构造函数。
public class Product{
public Tweet(){
}
}