无法为Product.class调用no-args构造函数

时间:2015-05-21 10:27:37

标签: java android gson getjson

我在我的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课程: 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(){}
}

我已经完成了与此相同的现有问题,根据我发现的答案,我将no arg构造函数添加到所有类中,但我仍然得到错误,请帮助我解决此问题

Json String:

{
  "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"
  }
}

4 个答案:

答案 0 :(得分:1)

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

public class Product{

    public Product(){
    }

}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您的模型具有私有属性,您必须为该模型创建setter或使用以下所有参数创建构造函数:

alter default privileges revoke all on functions from public;

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

答案 3 :(得分:-1)

只需为Bean类添加无参数构造函数。

public class Product{

  public Tweet(){
  }

}