标签:android xutils 下载 断点续传
//我主要是好奇Xutils是在哪里回调onLoading(),找老半天没找到,好不容易找到就写下来吧
前言:
1.仅对主要代码行进行摘要,提供大致流程
2.为便于理解,本文变量名不同于源码变量名,而是类名的驼峰式写法。如源码中:WorkRunnable mWorker,在本文中为 workRunnable
3.需要配合看Xutils的源码,可以让你开Xutils源代码时减少一定的难度
代码主干:
HttpFragment:
//HttpFragment是Xutils自带例子中的一个类
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,故以下方法实际上调用的是HttpHandler中的方法
//Thread可以执行的有两类接口 1.Runable()接口 2.Callable接口
//区别是分别调用run()、call()开线程;后者有返回值,可取消作业,可以返回异常
//然而想要用Thread调用Callable需要用FutreTask包装,FutureTask实现了Runable接口,done()方法在作业结束后执行,可用来判断下载成功、取消等状态;
//下面两行代码是初始化,不在执行顺序中
workRunnable{ call(){ return postResult(doInBackground(parmas)}};
futureTask = new FutureTask<Result>(workRunnable){ done( postResultIfNotInvoke())};
workRunnable.mParams = params;
priorityExecutor.execute(new PriorityRunnable(priority, futureTask));
PriorityExecutor:
//用线程池来开线程
threadPoolExecutor.execute(new PriorityRunnable(priority, futrueTask))
PriorityRunnable:
//futureTask是workRunnable包装类,调用的是workRunnable里的方法,即postResult(doInBackground(parmas);
futrueTask.run();
HttpHandler:
//终于运行HttpHandler的doInBackground了
httpHandler.doInBackground();
httpHandler.sendRequest(httpRequest);
httpResponse = client.execute(httpRequest, context);
handleResponse(httpResponse);
fileDownloadHandler.handleEntity(httpEntity, httpHandler, charset);
StringDownloadHandler:
//终于到下载处理阶段了。O(∩_∩)O哈哈~
while ((line = reader.readLine()) != null) {
httpHandler.updateProgress(total, current, false)
}
HttpHandler:
httpHandler.publishProgress(UPDATE_LOADING, total, current);
//values = new Progress[]{UPDATE_LOADING, total, current}; 利用Java的不确定数量的形参机制,即(Profress... )
//利用Handler机制通信
handler.obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values))
//priorityAsyncTask就是httpHandler
asyncTaskResult.priorityAsyncTask.onProgressUpdate(values);
//根据values[0]判断为正在下载
requestCallBack.onLoading(
Long.valueOf(String.valueOf(values[1])),//文件总大小
Long.valueOf(String.valueOf(values[2])),//已下载大小
isUploading);
//好了到这里就结束了,其他的开始下载、下载完、下载失败的回调函数的逻辑于此类似。
Xutils源码文件下载方法的调用流程
标签:android xutils 下载 断点续传
原文地址:http://blog.csdn.net/zhjali123/article/details/40015237