android - How do I put generic type for Gson's TypeToken? -
edit after experimenting while, know problem is. can't put generic type inside typetoken
(type type = new typetoken<customresponse<t>>(){}.gettype();
). when change t
pojoa
, can run app fine deserialize json pojoa
, not pojob
, pojoc
.
how put generic type typetoken? or, possible this:
if (t == pojoa) { type type = new typetoken<customresponse<pojoa>>(){}.gettype(); } else if (t == pojob) { type type = new typetoken<customresponse<pojob>>(){}.gettype(); } else if (t == pojoc) { type type = new typetoken<customresponse<pojoc>>(){}.gettype(); };
previous question: why parsenetworkresponse
return nothing when using parametrized type?
i suspect error in return (response<t>) response.success(gson.fromjson(json, typeoft), httpheaderparser.parsecacheheaders(response));
part, because print log.d("customrequest", json);
on previous line. (please @ my gsonrequest)
my pojo
public class customresponse<t> { t response; public customresponse(t response) { this.response = response; } public t getresponse() { return response; } }
my custom deserializer
public class pojoadeserializer implements jsondeserializer<customresponse<pojoa>> { @override public customresponse<pojoa> deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { //some deserializing thing return new customresponse<pojoa>(); } } public class pojobdeserializer implements jsondeserializer<customresponse<pojob>> { @override public customresponse<pojob> deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { //some deserializing thing return new customresponse<pojob>(); } } public class pojocdeserializer implements jsondeserializer<customresponse<pojoc>> { @override public customresponse<pojoc> deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { //some deserializing thing return new customresponse<pojoc>(); } }
my gsonrequest
public class customrequest<t> extends jsonrequest<t> { private final gson gson; private final type typeoft private final response.listener<t> listener; public customrequest(int method, string url, type typeoft, jsonobject params, response.listener<t> listener, response.errorlistener errorlistener) { super(method, url, params.tostring(), listener, errorlistener); this.typeoft = typeoft; this.listener = listener; gsonbuilder gsonbuilder = new gsonbuilder(); gsonbuilder.registertypeadapter(new typetoken<customresponse<pojoa>>(){}.gettype(), new pojoadeserializer()); gsonbuilder.registertypeadapter(new typetoken<customresponse<pojob>>(){}.gettype(), new pojobdeserializer()); gsonbuilder.registertypeadapter(new typetoken<customresponse<pojoc>>(){}.gettype(), new pojocdeserializer()); this.gson = gsonbuilder.create(); } @override protected void deliverresponse(t response) { listener.onresponse(response); } @override protected response<t> parsenetworkresponse(networkresponse response) { try { string json = new string(response.data, httpheaderparser.parsecharset(response.headers)); log.d("customrequest", json); return (response<t>) response.success(gson.fromjson(json, typeoft), httpheaderparser.parsecacheheaders(response)); } catch (unsupportedencodingexception e) { return response.error(new parseerror(e)); } catch (jsonsyntaxexception e) { return response.error(new parseerror(e)); } } }
the actual calling
public <t> void get(string url, response.listener<customresponse<t>> listener, response.errorlistener errorlistener) { type type = new typetoken<customresponse<t>>(){}.gettype(); customrequest<customresponse<t>> request = new customrequest<>(request.method.get, url, type, null, listener, errorlistener); volley.newrequestqueue(context.getapplicationcontext()).add(request); } public void getresponse() { string url = //some url; get(url, new response.listener<customresponse<pojoa>>() { @override public void onresponse(customresponse<pojoa> response) { //do } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { //do } }); }
i can print log.d("customrequest", json);
so means json response fine.
the problem return (response<t>) response.success(gson.fromjson(json, typeoft), httpheaderparser.parsecacheheaders(response));
.
is error when casting (response<t>)
.
or error on gson.fromjson(json, typeoft)
you have specify type
explicitly. there's type erasure in java, means @ runtime instances of t
replaced object
.
you should either write 3 different deserializers different response types(pojoa
, pojob
, pojoc
) or write generic one. should work:
public class genericconverter implements jsondeserializer<customresponse<?>> { private type typeofresponse; public genericconverter(type typeofresponse) { this.typeofresponse = typeofresponse; } public customresponse deserialize(jsonelement json, type typeoft, jsondeserializationcontext ctx) { customresponse response = new customresponse(); response.setresponse(ctx.deserialize(json.getasjsonobject().get("response"), typeofresponse)); return response; } }
and register appropriately:
gsonbuilder.registertypeadapter(new typetoken<customresponse<pojoa>>(){}.gettype(), new genericconverter(pojoa.class)); gsonbuilder.registertypeadapter(new typetoken<customresponse<pojob>>(){}.gettype(), new genericconverter(pojob.class)); gsonbuilder.registertypeadapter(new typetoken<customresponse<pojoc>>(){}.gettype(), new genericconverter(pojoc.class));
Comments
Post a Comment