无法为Product.class调用无参数构造函数 [英] Unable to invoke no-args constructor for Product.class
问题描述
我正在使用GSON和Volley库在我的android应用程序中进行联网,但是在使用Gson将Json响应转换为Model类时,却出现以下错误:
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:
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类: Product.java
these are my POJO classes i am using : 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
Pagination.java
public class Pagination {
private String first;
private String previous;
private String next;
private String last;
public Pagination(){}
}
Result.java
Result.java
public class Result {
private String id;
private Properties properties;
public Result{}
}
Properties.java
Properties.java
public class Properties {
private String qbcode;
private String name;
private String purchasedate;
private String vendor;
private String thumbnail;
public Properties(){}
}
我已经解决了类似这样的现有问题,根据答案,我发现我向所有类中添加了no arg构造函数,但仍然出现错误,请帮助我解决该问题
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
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"
}
}
推荐答案
为所有类添加默认构造函数.示例:
Add default constructor for all classes. Example:
public class Product{
public Product(){
}
}
这篇关于无法为Product.class调用无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!