RxJava2. x+ReTrofit2. X multi thread download file sample code

Write before:

Received the company's demand: to do an APK upgrade function, the principle is actually very simple. Baidu also has a lot of examples, but most of them use the framework, or httpurlconnection. I really don't want to do so. I just watched rxjava 2 for two days x+ReTrofit2. x. It is said that these two frameworks are the most popular asynchronous request frameworks at present. This article uses rxjava 2 x+ReTrofit2. X realizes the function of multi-threaded downloading files. If rxjava2 x+ReTrofit2. If you don't know much about X, please read the relevant documents first. The great God is here. Please ignore it.

Train of thought analysis:

The idea is concise and clear, which is mainly divided into the following four steps

1. Get the server file size 2. Plan the number of threads according to the file size 3. Merge the downloaded content into a complete file 4. Call installation and install APK Function realization

Come on, next is your favorite code link

1. First look at the quotation

2. Construct a download interface downloadservice class

3. A retrofithelper is encapsulated for ease of use Class, mainly used for:

a) Instantiate okhttpclient and retrofit

b) Package download method. I use three download threads for this download. There is no dynamic allocation. You can dynamically allocate the number of threads according to your own needs

4. Call Download

Note: the call and download are carried out in mainactivity. In order to be intuitive, we have encapsulated the progress interceptor to facilitate the progress display. However, this article does not describe the implementation process of the progress interceptor, and you can leave a message if necessary.

a) Implement listening object

b) Call the encapsulated retrofithelper to download

Note: the last one is the APK installation method

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>