//我主要是好奇Xutils哪里回调onLoading(),查找等了很久也没找到,果然easy查找只是把它写下来
前言:
1.代码摘要只有主线,提供一般流程
2.为了易于理解,码变量名,而是类名的驼峰式写法。如源代码中:WorkRunnable mWorker,在本文中为 workRunnable
3.须要配合看Xutils的源代码,能够让你看Xutils源代码时降低一定的难度
代码主干:
HttpFragment:
//HttpFragment是Xutils自带样例中的一个类
httpUtils = new HttpUtils();
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中的方法
//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();
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);
//好了到这里就结束了,其它的開始下载、下载完、下载失败的回调函数的逻辑于此类似。
版权声明:本文博主原创文章。博客,未经同意不得转载。