Today in the Volley demo (Https://github.com/smanikandan14/Volley-demo), found that the custom gsonrequest code is not complete, paste a full here.
Public classGsonrequest<t>extendsRequest<t> { PrivateGson Mgson; PrivateClass Mjavaclass; PrivateResponse.listener<t>Mlistener; PublicGsonrequest (intmethod, String URL, class<t>CLS, String requestbody, Response.listener<T>Slistener, Response.errorlistener Listener) { Super(method, URL, listener); Mjavaclass=CLS; Mlistener=Slistener; Mgson=NewGson (); } @OverrideprotectedResponse<t>Parsenetworkresponse (networkresponse response) {Try{String jsonstring=NewString (Response.data, Httpheaderparser.parsecharset (response.headers)); T Parsedgson=(T) Mgson.fromjson (jsonstring, Mjavaclass); returnresponse.success (Parsedgson, Httpheaderparser.parsecacheheaders (Response)); } Catch(unsupportedencodingexception e) {returnResponse.error (NewParseError (e)); } Catch(jsonsyntaxexception je) {returnResponse.error (NewParseError (JE)); }} @Overrideprotected voidDeliverresponse (T response) {mlistener.onresponse (response); }
Briefly explain the role of several fields.
Mjavaclass is used when deserializing into objects through JSON;
Mlistener inside Onresponse is the callback function that handles the correct return value;
The specific implementation can also refer to the Jsonrequest class in volley.
[Android] Open source framework volley custom Request