【问题标题】:Unable to invoke no-args constructor for Product.class无法为 Product.class 调用无参数构造函数
【发布时间】: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 我已经编辑了我的问题

标签: java android gson getjson


【解决方案1】:

为所有类添加默认构造函数。示例:

public class Product{

    public Product(){
    }

}

【讨论】:

【解决方案2】:

问题解决了,我对 Gson 做了一个愚蠢的调用,因为我正在获取一个包含其他类(Result.class 等)列表的 Product 类,但我正在将 Product[].class 发送到 Gson 以进行转换,因此它抛出了异常.

【讨论】:

    【解决方案3】:

    您的模型具有私有属性,您必须为其创建 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;
        }
    }
    

    或

    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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 2020-02-14
      • 2022-12-05
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2019-10-19
      • 1970-01-01
      相关资源
      最近更新 更多