Android-async-http Request Analysis < original >

Source: Internet
Author: User

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 ....



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.