Retrofit rxjava realizes image downloading, saving and displaying examples

First, let's take a look at the general usage of retrofit. Without using rxjava, we return call by default.

However, if we want to use rxjava, we need to redefine our method as follows:

An observable is returned. The method name is very intuitive, that is, downloading pictures from the network. The parameter is the URL path of the pictures

After defining the request interface, let's create the retrofit object

Next, we create an instance of the serviceapi just defined, and create it through the retrofit created above

OK, now we can call the downloadpicfromnet method we just defined through serviceapi to download a picture. We can baidu a picture at will and copy the picture address for testing.

The above example implements a process of downloading, saving and displaying. Compared with the traditional asynctask, the code is very concise without many callbacks. Of course, in actual use, you can also encapsulate a layer and put the creation of retrofit and serviceapi in an implementation class of serviceapiimpl.

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