android 无法为Product.class调用无参数构造函数

6ovsh4lw  于 2023-04-10  发布在  Android
关注(0)|答案(3)|浏览(81)

我正在使用GSON和Volley库在我的Android应用程序中联网,但在使用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类: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

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(){}
}

我已经通过了现有的问题,这是一样的,作为每一个答案,我发现我添加了无参数构造函数的所有类,但我仍然得到的错误,请帮助我解决这个问题
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"
  }
}
3z6pesqy

3z6pesqy1#

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

public class Product{

    public Product(){
    }

}
uurity8g

uurity8g2#

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

ilmyapht

ilmyapht3#

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

}

相关问题