android - How to use the RestErrorHandler with AndroidAnnotations? -
how correctly utilize resterrorhandler? utilize androidannotations 3.0.1 whitch uses springresttemplate android.
i utilize this illustration start , works expected how supposed utilize it? mean if grab exception custom errorhandler how notify activity/fragment? custom errorhandler approach be:
@background public void getauth() { user u = restclient.getauth(auth); //the of import part here //how know here error happend except check user null? //more of import how pass httperrorcode , custom headerfield? if (u != null) { log.d("xxxx", "authtoken: " + u.getauthtoken()); } dolater(); eventmanager.fire(events.load.dismiss); } with default errorhandler approach be:
@background public void getauth() { seek { user u = restclient.getauth(auth); //important part here happens failure log.d("xxxx", "authtoken: " + u.getauthtoken()); dolater(); } grab (restclientexception e) { log.e("xxxx", "error.................."); }finally{ eventmanager.fire(events.load.dismiss); } } isn't bad handle httperrorcodes in activity/fragment? if should phone call it?
of course of study unchecked exception can thrown customerrorhandler , pass parameters within exception still have no thought how access headerfields of response.
may not elegant solution, please weigh in if has improve approach.
of course of study unchecked exception can thrown customerrorhandler , pass parameters within exception still have no thought how access headerfields of response.
resttemplate throws nestedruntimeexception, httpclienterrorexception wrapped in nestedruntimeexception, when client error occurs. can checked cast httpclienterrorexception lets access of (if not all) info require.
i utilize illustration start , works expected how supposed utilize it? mean if grab exception custom errorhandler how notify activity/fragment?
in custom error handler throw new custom exception (depending on situation i'm trying handle) contain friendly message activity catches , straight displays. example, if server sends unauthorized 401 response, throw new custom exception message "you have logged out, please log in continue" activity displays/handles accordingly.
if (runtimeexception instanceof httpclienterrorexception) { httpclienterrorexception exception = (httpclienterrorexception) runtimeexception; if (exception.getstatuscode().equals(httpstatus.unauthorized)) { throw new displaymessageexcpetion("you have logged out, please log in continue"); } else if (exception.getstatuscode().equals(httpstatus.bad_gateway)) { etc... //more of import how pass httperrorcode , custom headerfield?
above works if need access http status , body of response, if need access custom header fields need provide own implementation of
void handleerror(clienthttpresponse response) throws ioexception; from responseerrorhandler , set error handler on resttemplate. source of defaultresponseerrorhandler should give thought of how it. refer this getting/setting resttemplate service interface.
android error-handling resttemplate android-annotations
No comments:
Post a Comment