Unable to invoke no-args constructor for Product.class

后端 未结 4 692
梦毁少年i
梦毁少年i 2021-01-19 14:46

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 err

相关标签:
4条回答
  • 2021-01-19 15:25

    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.

    0 讨论(0)
  • 2021-01-19 15:32

    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;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-19 15:43

    Add default constructor for all classes. Example:

    public class Product{
    
        public Product(){
        }
    
    }
    
    0 讨论(0)
  • 2021-01-19 15:47

    Just add an no argument constructor for the Bean class.

    public class Product{
    
      public Tweet(){
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题