Android Combat--rxjava2+retrofit+rxbinding unlock all new poses

Source: Internet
Author: User

This article has authorized the public number: Yang (hongyangandroid) original debut.

As the mainstream of the third-party Framework RX series, not learning is not good ah, for beginners, may rxjava looks difficult, it is more difficult to use, but you have to know, the more complex things can often solve the more complex problems, it is possible that you apply in the project, perhaps you in the interview, will be a long distance from the junior engineer. This course needs everyone to have retrofit foundation, if want to learn retrofit classmate can view my blog, nonsense say, Hensen teacher Drive.

The exact words are: RXJAVA2 is an event-based, asynchronous library that uses observable sequences on a Java virtual machine. Don't understand it's okay, can be likened to our asynctask, so we understand more

RxJava Portal: Https://

The Observer pattern is the core point used by Rxjava, mastering this pattern, can understand Rxjava simpler, the observer pattern is simply called "subscription-release" mode, for example, when you subscribe to a milk store for breakfast milk (subscription process), as long as the milk shop production of milk, Will send you to the past (the release process). There is only one in the dairy, but the number of subscribers can be many, this is a one-to-many relationship, as long as the milk shop released milk, then the subscribers will receive milk. In exchange for Rxjava inside, the dairy is the Observer (Observable), the person who subscribes is the Observer (Observer)

Here we give an example of a school roll-name, first creating what we call the observer and the Observer

You can think about, according to the above introduction, students and teachers, who is the Observer, who is the observer, the following look at the code to you analysis

You can see through the code that different interfaces are implemented separately

1, the teacher is the observer, he needs to implement the interface method

    • Subscribe/UNSUBSCRIBE: Store/Remove observers from the collection
    • Publishing: Looping through observers, invoking observer methods

2, the student is the observer, then we only need to give him a name, realizes the Observer the method can

Finally, we associate the observer with the observed, Lessonstart (class)

The code is very simple, we simulated a teacher and Xiao Ming classmate and Little Red classmate, the teacher already knew to see two people come, then can start roll call, below through log print out information

First of all, I put out the third-party dependency library We used in the back, lest we forget to say it, we should

Next you need to add networking permissions

Finally we go back to the point, after reading the above example, we can know that Rxjava is the model of this subscription and release, in order to replace our Rxjava code should be how? And, of course, by the observer subscribing to the Observer.

The realization of our specific observer and observer is, of course, created.

The OnNext method is our release process, and we look at the creation of the observer to know what's going on.

We can find that the method of the observer's creation is corresponding to the observer, that is, what we release, we can receive the subscription information in the Observer, then we can write our logic in the code, so that we have basically used the Rxjava, print out the information through the log

1, people like cool, show off, of course, Rxjava is also the human mentality, is the chain programming, the following code can be a perfect substitute for all the code above

Here I have written more than two methods, namely. Subscribeon ( ()) and. Observeon (Androidschedulers.mainthread ()), which is one of the benefits of Rxjava, He can switch threads manually, both of which indicate that the method by which the observer creates the implementation is placed on the IO thread, which is the child thread, because the network request is usually called by the Observer, and then the network requests must run on the child thread, and when the network request is received, it is published. The UI updates must be performed on the main thread, which is the code mainthread above, which is shown in the viewer through TextView and other controls. If you do not study Rxjava, basically these two methods have become a fixed wording, which is also a lot of beginners forget to add the method

2, over time, human like concise, like custom services, Qiao, Rxjava also give you satisfaction, the following code, the implementation of the method is corresponding to the above implementation method, we see the parameters to know which corresponds to which, you can pass new Consumer, Do not need to implement the method you can not write, look more concise, here I for the convenience of everyone to see, are new out, consumer is the meaning of consumers, can be understood as the consumption of onnext and so on and other events

Oh, yes, we forgot to print the log information, and I can't deny the fact that I'm handsome.

Of course, you think as long as you praise me a handsome on the line, then you can also be sent to the observer by the following methods

Here's the case use our usual simplest needs, see Know (Picture will card, effect everyone brain complement)

Here is the entire code implementation ideas, I will comment on the code below the points to note, the code I directly posted out, there is a good word, become a great God, you must first learn to read someone else's code, hahaha, my code reading should be no problem

1. Operator

    • Like this interval, take, map, Observeon, Doonsubscribe, subscribe are all belong to the Rxjava operator, the simple is to implement a method, the functions are packaged up
    • Rxjava supports a lot of operators, many of them are simple to use, but the combination is very complex, powerful, specific classification

2. Introduction to Operators

    • Interval: Delay a few seconds and start executing every few seconds
    • Take: Over how many seconds to stop execution
    • Map: type conversion, because it is a countdown, the case needs to reverse the countdown number
    • Observeon: Running on the main thread
    • Doonsubscribe: In the course of execution
    • Subscribe: Subscribe

Rxjava and retrofit use, more like our asynctask, get data over the network and then update the UI through handler

Human beings always like to look at the picture to talk, Qiao, I give you, I can only come up with my excellent art technology to you drawing

①bean Object

    • Here we are using a Httpbin post interface, you can try it on its website, where the Netbean is the data returned by the request, the Gsonformat generated by the


    • This returns the observable object, which is the observer of our Rxjava.

The realization of ③rxjava+retrofit

1, Retrofit

    • Rxjava with retrofit the words must be added to the retrofit Addcalladapterfactory (Rxjava2calladapterfactory.create ())

2, RxJava

    • We send the data by just way
    • Flatmap method is used for data format conversion method, the following parameters Userparam and observablesource< netbean>, parameter one represents the original data, parameter two represents the transformed data, then is by sending network parameters, Convert to data returned by the network, call retrofit

This case is actually the user to add the shopping cart, the first will be stored locally, and then found that if there is no network, then no way to submit to the server, you can only wait for the next time the network with the local database and server data merge to implement the upload to the server, Here we paste Rxjava and retrofit code, no other code, no nonsense to say,

This uses the merge operator, which means to merge two observablesource into a single observablesource, and the final print result is

Rxbinding is also used to make the interface look more concise, leaving the traditional methods of Findviewbyid and Setonclicklistener, without any declaration, as long as the addition of dependencies can be used directly

The case here is that when we are typing in edittext real-time search, the user may be typing very fast, middle-aged clothing so we do not have to send network requests, request search results, we can be the user after typing stop delay 500 milliseconds to send a search request


    • Rxtextview.textchanges (edittext): rxbinding usage
    • Debounce: Indicates the delay after how many seconds to execute
    • Switchmap: It's also data conversion, and the difference between the Flatmap is clearly explained in the comments.


This case is very simple, when the user has been clicking a button, we should not always call access to the network request, but in 1 seconds, only one time to execute a network request

SOURCE download

I heard that serious students will learn a lot of oh, for the Rxjava operator or recommend you through the official website of the wiki to further study, back to the force only when Rxjava used in the project, you will appreciate its benefits, but also let you and junior engineers have a certain distance, like a lot of rxbus, Rxpermission, Rxandroid, many people will doubt to learn it, no doubt it is necessary to learn, technology is constantly updated, only when your technology to keep up with the times, you can and big QQ relish, the above is purely nonsense, of course, there is time I will also learn the RX series, If you like my friends can pay attention to my blog, or add group of people to study together

Android Combat--rxjava2+retrofit+rxbinding unlock all new poses

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.