When Android-async-http sends a request, the place where the request is actually executed is in the Asynchttprequest class, with two methods:
Private void makerequest()throwsIOException {if(iscancelled ()) {return; } //Fixes #115 if(Request.geturi (). Getscheme () = =NULL) { //subclass of IOException so processed in the caller Throw NewMalformedurlexception ("No valid URI scheme was provided"); } HttpResponse Response=Client.execute (request, context); if(!iscancelled () && ResponseHandler! =NULL) {responsehandler.sendresponsemessage (response); } } Private void makerequestwithretries()throwsIOException {BooleanRetry =true; IOException cause=NULL; Httprequestretryhandler Retryhandler=Client.gethttprequestretryhandler (); Try { while(retry) {Try{makerequest (); return; } Catch(unknownhostexception e) {//switching between Wi-Fi and mobile data networks can cause a retry which then results in an unknownhostexception //While the Wi-Fi is initialising. The retry logic would be a invoked here, if this isn't the first retry//(to assist in genuine cases of unknown host) which seems better than outright failureCause =NewIOException ("Unknownhostexception exception:" +e.getmessage ()); Retry= (Executioncount > 0) && retryhandler.retryrequest (cause, + +)Executioncount, context); } Catch(NullPointerException e) {//there ' s a bug in HttpClient 4.0.x so on some occasions causes//defaultrequestexecutor to throw a NPE, see// http://code.google.com/p/android/issues/detail?id=5255Cause =NewIOException ("NPE in HttpClient:" +e.getmessage ()); Retry= Retryhandler.retryrequest (cause, + +)Executioncount, context); } Catch(IOException e) {if(iscancelled ()) {//Eating exception, as the request was cancelled return; } cause=e; Retry= Retryhandler.retryrequest (cause, + +)Executioncount, context); } if(Retry && (ResponseHandler! =NULL) {responsehandler.sendretrymessage (executioncount); } } } Catch(Exception e) {//catch anything else to ensure failure message is propagatedLOG.E ("Asynchttprequest", "Unhandled exception Origin cause", E); Cause=NewIOException ("Unhandled Exception:" +e.getmessage ()); } //cleaned up to throw IOException Throw(cause); }
MakeRequestis the method of sending the request. = Client.execute (request, context); This is the execution of the requests, and response is the result of the request return. In theory, this time the request was fulfilled, so why should there be
makerequestwithretries () method exists?
The reason is: the execution of the = Client.execute (request, context) sometimes fails, it may be a network card, or another reason, so you have to retry this method several times. This is the core of this approach.
If the request succeeds, then return directly, otherwise it will go into the exception (Client.execute (request, context) throws .... Retry (A Boolean variable that is the loop-judging condition for the loop-sending request) is changed in the handling of the exception.
New IOException ("NPE in HttpClient:" += Retryhandler.retryrequest (cause, ++executioncount, context);
Retryhandler.retryrequest (Cause, ++executioncount, context); This method is the method provided in the Apach package and requires a view of the Android API.
The introduction to Retryhandler in the API is:
Simple introduction is to determine whether to retry, the specific method of judging did not find ....