java - Using Volley and Gson: Parse item and items list -
one thing i've never liked gson fact have pass class object or typetoken based on if you're getting item or list of items. now, when trying use volley gson problem persists , i'm trying make gsonrequest class can used both things.
my solution quite ugly, 2 different constructors: 1 getting class<t>
parameter , 1 getting type
parameters. then, in parsenetworkresponse
, gson.fromjson
called either 1 of fields, keeping in mind 1 has null
.
any idea of how implement in better way? (i don't having gsonrequest
, gsoncollectionrequest
almost-equal classes)
my code, here:
public class gsonrequest<t> extends request<t> { private final gson gson; private final class<t> clazz; private final type type; private final listener<t> listener; private final map<string, string> headers; private final map<string, string> params; public gsonrequest(int method, string url, gson gson, class<t> clazz, map<string, string> headers, map<string, string> params, listener<t> listener, errorlistener errorlistener) { super(method, url, errorlistener); this.gson = gson; this.clazz = clazz; this.type = null; this.listener = listener; this.headers = headers; this.params = params; } public gsonrequest(int method, string url, gson gson, type type, map<string, string> headers, map<string, string> params, listener<t> listener, errorlistener errorlistener) { super(method, url, errorlistener); this.gson = gson; this.clazz = null; this.type = type; this.listener = listener; this.headers = headers; this.params = params; } @override public map<string, string> getheaders() throws authfailureerror { return this.headers != null ? this.headers : super.getheaders(); } @override protected map<string, string> getparams() throws authfailureerror { return this.params != null ? this.params : super.getparams(); } @override protected response<t> parsenetworkresponse(networkresponse response) { try { if (this.clazz != null) { return response.success( this.gson.fromjson(new string(response.data, httpheaderparser.parsecharset(response.headers)), this.clazz), httpheaderparser.parsecacheheaders(response)); } else { return (response<t>) response.success( this.gson.fromjson(new string(response.data, httpheaderparser.parsecharset(response.headers)), this.type), httpheaderparser.parsecacheheaders(response)); } } catch (jsonsyntaxexception e) { e.printstacktrace(); return response.error(new parseerror(e)); } catch (unsupportedencodingexception e) { e.printstacktrace(); return response.error(new parseerror(e)); } } @override protected void deliverresponse(t response) { this.listener.onresponse(response); } }
you can create new gsonrequest using typetoken type parameter.
use generic gsonrequest gsonrequest.
create simple request gson class...
new gsonrequest<myclass>(request.method.get, uribuilder.build().tostring(), myclass.class, null, mresponselistener, mreponseerrorlistener));
or create type arraylist...
type type = new typetoken<arraylist<myclass>>() {}.gettype(); new gsonrequest<arraylist<myclass>>(request.method.get, uribuilder.build().tostring(), type, null, mresponselistlistener, mreponseerrorlistener));
Comments
Post a Comment