Solution to the problem of Android retro file download progress display

overview

The usage of retrofit is described in detail in this article. In retrofit, we can download files through ResponseBody. However, there is no interface to display the download progress in retrofit. In the project, if the user downloads a file, the download progress cannot be displayed to the user in real time, so the user experience is also very poor. Then, let's introduce how to track the download progress in real time in Retrofit for file download.

demonstration

Implementation of retrofit file download progress update

In retrofit 2.0, it relies on okhttp, so if we need to solve this problem, we need to start with okhttp. There is a dependency package okio in okhttp. Okio is also developed by square company. It is a supplement to java.io and java.nio. It is easier to access, store and process data. Here you need to use the source class in okio. Here, source can be regarded as InputStream. The detailed use of okio is not introduced here. Let's take a look at the specific implementation. Here, we first write an interface to monitor the progress of downloading. For file download, we need to know the download progress, the total size of the file, and whether the operation is completed. So there is the following interface.

For file download, we need to override some methods in the ResponseBody class.

In the progressresponsebody class above, we calculate the number of bytes of the read file and call the progresslistener interface. Therefore, the progresslistener interface runs in the child thread. Let's take a look at how to use this progressresponsebody.

We use our custom progressresponsebody by adding an interceptor to okhttpclient. And here we can implement the progresslistener interface. To get the download progress. However, there is still a problem here. Just now, the progresslistener interface runs in the sub thread. That is to say, we cannot perform UI operations in the progresslistener interface. When we get the progress of file download, we often need a progress bar for UI display. Obviously, this is not the result we want. At this time, we need to use handler. We can send the progresslistener data in the child thread to the UI thread for processing through the handler. That is, our operation in the progresslistener interface only sends its parameters through the handler. Obviously, in the above code, we send messages through progresshandler. Let's take a look at the specific operation. Here we create an object to store the parameters in progresslistener.

Then we are creating a progresshandler class.

The progresshandler above is an abstract class. Here we need to send and process messages through the handler object. Therefore, two abstract methods SendMessage and handlemessage are defined. Then, an abstract method onprogress is defined to handle the display of download progress, which we need to call in the UI thread. Finally, a responsehandler internal class inherited from handler is created. To avoid memory leaks, we use the static keyword. Next, create a downloadprogresshandler class, which inherits from progresshandler and is used to send and process messages.

Here we call the abstract method onProgress after receiving the message, so we only need to create a DownloadProgressHandler object to implement onProgress. For the above analysis, let's see how to use it.

summary

For the above implementation, we can see that it is implemented through okhttpclient. It is precisely because it relies on okhttp in retrofit 2.0 that retrofit also has the function of okhttp. Using this feature, we can configure our retrofit by customizing okhttpclient.   

Source code download: https://github.com/lijiangdong/retrofit-example

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
分享
二维码
< <上一篇
下一篇>>