android volley - ResponseDelivery Implementations -
i implement common error handler handle errors of particular status code. every request put on queue have same behavior. 1 scenario upon 401. when 401 status received, show login dialog , replay original request without activity launched request having know it. looking @ implementing custom responsedelivery identical executordelivery. when went this, discovered request.finish method package private. there better way implement kind of behavior volley? thanks
start following:
public abstract class baserequest<t extends errorresponse> extends request<t> { @override protected response parsenetworkresponse(networkresponse response) { jsonstring = new string(response.data); t parsedresponse = null; try { parsedresponse = parsejson(jsonstring); // can handle type of common errors server if (parsedresponse != null && parsedresponse.geterrorcode() == 401) { return response.error(new sessionexpirederror(parsedresponse.geterrormessage())); } } catch (jsonsyntaxexception e) { ln.e(e, "jsonsyntaxexception"); ln.e(jsonstring); return response.error(new volleyerror(e)); } return response.success(parsedresponse, httpheaderparser.parsecacheheaders(response)); } } public class errorstatusresponse { private int errorcode; private string errormessage; ... }
all requests extending baserequest checked against errors specify before result delivered.
moving on, cannot start activity or present dialog without context (and don't want pass context
responsedelivery. create baseactivity purpose:
public abstract class baseactionbaractivity extends actionbaractivity implements response.errorlistener { @override public void onerrorresponse(volleyerror error) { // if childactivitiy wants override method, make sure call super.onerrorresponse if (error instanceof noconnectionerror) { noconnection(); } else if (error instanceof sessionexpirederror) { sessionexpired(); // startforresult loginactivity here } else { ln.e(error); } } }
in order fire previous request again, i'd smth this:
// childactivity public void firerequest() { api.getmyitemsfromserver(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == subactivity_session_expired) { api.getmyitemsfromserver(); } }
or save last unsuccessful request in baseactivity
, handle in onactivityresult()
Comments
Post a Comment