I am mainly curious xutils where callback onloading (), look for a long time did not find, sure enough easy to find just write it down
Objective:
1. Code summary only mainline, provide general flow
2. In order to be easy to understand, the code variable name, but the class name of the camel style notation. In the source code: Workrunnable Mworker, in this article for workrunnable
3. Need to cooperate to see Xutils source code, can let you see Xutils source code to reduce a certain difficulty
Code skeleton:
Httpfragment:
Httpfragment is a class in the Xutils self-contained sample
Httputils = new Httputils ();
Httputils.send (Mthord, URL, params, callback{onStart (), onloading (), onsuccess (), OnFailure,})
httputils:
HttpRequest = new HttpRequest (method, URL); httputils.sendrequest (request, params, callback); HttpHandler = new Handler <T> (HttpClient, HttpContext, Resoponsetextcharset, CallBack), Httprequest.setrequestparams (params, HttpHandler); Httphandler.executeonexcutor (priorityexecutor, request);
Priorityasyntask = = HttpHandler:
HttpHandler extends Priorityasynctask, so the following method is actually called the method in HttpHandler
Thread can run two types of Interface 1.Runable () interface 2.Callable interface//difference is called the run (), call () thread, the latter has a return value, can cancel the job, can return the exception// However, to call callable with thread requires a futretask wrapper. Futuretask implements the Runable interface, and the Done () method runs at the end of the job, which can be used to infer the successful and canceled status of the download;
The following two lines of code are initialized. Not in Run orderworkrunnable{call () {return Postresult (Doinbackground(Parmas)}}; Futuretask = new Futuretask<result> (workrunnable{Done (Postresultifnotinvoke ())};
workrunnable.mparams = params;Priorityexecutor.execute (new priorityrunnable (priority, Futuretask));
Priorityexecutor:Thread pool thread Threadpoolexecutor.execute(New Priorityrunnable (priority, Futruetask))
priorityrunnable:
Futuretask is the Workrunnable wrapper class, called the Workrunnable method, that is, the Postresult (Doinbackground(Parmas);
Futruetask.run ();
HttpHandler:The final execution of HttpHandler's doinbackground was Httphandler.doinbackground (); Httphandler.sendrequest (HttpRequest); HttpResponse = Client.execute (HttpRequest, context); Handleresponse (HttpResponse); filedownloadhandler.handleentity (Httpentity, HttpHandler, CharSet);
Stringdownloadhandler:Finally to the download processing stage.
O (∩_∩) o haha ~ while (line = Reader.readline ()) = null) {HttpHandler.UpdateProgress(Total, current, false)}
HttpHandler:Httphandler.publishprogress (update_loading, total, current);
values = new progress[]{update_loading, total, current}; Using Java's indeterminate number of formal mechanisms, i.e. (profress ...)//using handler mechanism to communicatehandler.obtainmessage (message_post_progress,New Asynctaskresult<progress> ( This, values))
Priorityasynctask isHttpHandlerAsynctaskresult. Priorityasynctask.onprogressupdate (Values)///based on values[0] inferred as downloading requestcallback.onloading (long.valueof (string.valueof (values[1)),//Total file size Long.valueof (String.valueof (values[2])),//downloaded size isuploading);//Well, it's over here, and the rest begins. The logic for downloading, downloading, and downloading the failed callback function is similar.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Xutils Call Flow Source code file download method