java - No-args constructor for class XXX does not exist -


i new android development. here, making get call -

    protected string doinbackground(string... params) {         list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(1);         namevaluepairs.add(new basicnamevaluepair("email", "guest@example.com"));          jsonhttpclient jsonhttpclient = new jsonhttpclient();         productdetail[] products  = jsonhttpclient.get(serviceurl.product, namevaluepairs, productdetail[].class);          return null;     } 

this get call in jsonhttpclient file -

 public <t> t get(string url, list<namevaluepair> params, final class<t> objectclass) {     defaulthttpclient defaulthttpclient = new defaulthttpclient();     string paramstring = urlencodedutils.format(params, "utf-8");     url += "?" + paramstring;     httpget httpget = new httpget(url);           httpget.setheader("accept", "application/json");         httpget.setheader("accept-encoding", "gzip");         httpget.setheader("authorization", "bearer <code>");          httpresponse httpresponse = defaulthttpclient.execute(httpget);         httpentity httpentity = httpresponse.getentity();         if (httpentity != null) {             inputstream inputstream = httpentity.getcontent();             header contentencoding = httpresponse.getfirstheader("content-encoding");             if (contentencoding != null && contentencoding.getvalue().equalsignorecase("gzip")) {                 inputstream = new gzipinputstream(inputstream);             }              string resultstring = convertstreamtostring(inputstream);             inputstream.close();             return new gsonbuilder().create().fromjson(resultstring, objectclass);          }      return null; } 

and productdetail class -

public class productdetail {      public int id;     public string name;  } 

on running this, getting below error -

no-args constructor class com.compa.productdetail not exist. register instancecreator gson type fix problem. 

this thrown on line in jsonhttpclient file -

return new gsonbuilder().create().fromjson(resultstring, objectclass); 

can on this?

in web api, creating json (proddetails c# ienumerable object) -

json = jsonconvert.serializeobject(proddetails); var response = this.request.createresponse(httpstatuscode.ok); response.content = new stringcontent(json, encoding.utf8, "application/json"); return response; 

the structure of response json -

[    {        "id": 1,        "name": "first"    },    {        "id": 2,        "name": "second"    }           ]  

the gson user guide (https://sites.google.com/site/gson/gson-user-guide) tells behaved class (meant serialization , deserialization) should have no argument constructor. if not there, advises use instancecreator.

even if not have constructor, gson create objectconstructor class. not safe , has it's own limitations. question on goes more details: is default no-args constructor mandatory gson?

note: please see if inner class, must have constructor explained in documentation.

edit: json array. need have specified number of array objects in containing class. can following , cast:

public class productdetailarray {      public productdetailarray[] array;      public static productdetail {         public productdetail() {} // can make constructor private if don't want instantiate          public int id;         public string name;      } } 

once cast json before:

productdetailarray obj = gsonbuilder.create().fromjson(response, productdetailarray.class);  productdetail 1 = obj.array[0]; productdetail 2 = obj.array[1]; 

and can manipulation.. should using gson.fromjson() rather gsonbuilder


Popular posts from this blog