转载请注明:http://blog.youkuaiyun.com/w525721508/article/details/77992988
写在前面:
接到公司需求:要做一个apk升级的功能,原理其实很简单,百度也一大堆例子,可大部分都是用框架,要么就是HttpURLConnection,实在是不想这么干。正好看了两天的RxJava2.x+ReTrofit2.x,据说这俩框架是目前最火的异步请求框架了。固本文使用RxJava2.x+ReTrofit2.x实现多线程下载文件的功能。
如果对RxJava2.x+ReTrofit2.x不太了解的请先去看相关的文档。
大神至此请无视。
思路分析:
思路及其简洁明了,主要分为以下四步
- 1.获取服务器文件大小.
- 2.根据文件大小规划线程数量.
- 3.根据下载内容合并为完整文件.
- 4.调用安装,安装apk.
功能实现
来,接下来是你们最喜欢的撸代码环节
1.首先看引用
compile 'io.reactivex:rxjava:latest.release'
compile 'io.reactivex:rxandroid:latest.release'
//network - squareup
compile 'com.squareup.retrofit2:retrofit:latest.release'
compile 'com.squareup.retrofit2:adapter-rxjava:latest.release'
compile 'com.squareup.okhttp3:okhttp:latest.release'
compile 'com.squareup.okhttp3:logging-interceptor:latest.release'
2.构造一个下载接口DownloadService.class
public interface DownloadService {
@Streaming
@GET
//downParam下载参数,传下载区间使用
//url 下载链