Unable to invoke no-args constructor for Product.c

2020-04-10 00:56发布

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.

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

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

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

The 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条回答
淡お忘
2楼-- · 2020-04-10 01:28

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.

查看更多
仙女界的扛把子
3楼-- · 2020-04-10 01:49

Just add an no argument constructor for the Bean class.

public class Product{

  public Tweet(){
  }

}
查看更多
Anthone
4楼-- · 2020-04-10 01:51

Add default constructor for all classes. Example:

public class Product{

    public Product(){
    }

}
查看更多
够拽才男人
5楼-- · 2020-04-10 01:52

Your model having private attribute you must create setter for the same or create constructor with all parameter like below:

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

}
查看更多
登录 后发表回答