RxJava + Retrofit Complete Network request

Source: Internet
Author: User

1. Preface

This article is based on the use of Rxjava, retrofit, if the Rxjava or retrofit do not understand the Jianyo can first understand the Rxjava, retrofit usage and then see this article.

The use of Rxjava and retrofit was described separately before this article:

    • Use of Android Retrofit 2.0

    • Use of Android Rxjava (i) Basic usage

1. function

After understanding the use of RxJava and retrofit respectively, RxJava, retrofit collocation will no longer talk.

Let's take a look at how to complete a network request using retrofit:

2.1 Using Retrofit alone

1. Write a service first

1 Interface MyService {  @GET ("User/login")  call<userinfo> Login (2           @Query ("username") String username,3           @Query ("Password") string password4  ); 5 }

2. Get call to perform network requests

These are the practices of retrofit when used alone.

How is the combination of retrofit and Rxjava used? Here is the point of this article.

2.2 Rxjava+retrofit Complete Network request

1, add dependencies. The first four are RxJava, rxandroid, retrofit, and Gson, the last of which is the new addition, the use of RxJava + retrofit needs to use the last package.

Compile ' io.reactivex:rxjava:x.y.z' io.reactivex:rxandroid:1.0.1' com.squareup.retrofit2: retrofit:2.0.2 ' com.squareup.retrofit2:converter-gson:2.0.2 'com.squareup.retrofit2: adapter-rxjava:2.0.2 '

Note: The last three packages must have the same version number, which is 2.0.2.


2. Write a service with login method

The return value of the login method here is the observable type, compared to the previous service.

Observable ... Do you think it is familiar, this goods is not used in Rxjava before the listener?

3, use observable to complete a network request, log in successfully save data to local.

RxJava + Retrofit form, the Retrofit encapsulates the request into Observable, calls OnNext () after the request ends, or calls OnError () after the request fails.

As you can see, after invoking the service's login method, we get the observable object, execute the network request in the new thread, switch to the IO thread to execute the action to save the user information after the request succeeds, and then switch to the main thread to execute the request failure onerror (), The request succeeded OnNext ().

The logic of the whole is very clear in a chain, even if there are other requirements can also be added to the inside, does not affect the simplicity of the code.

(Finally, give an example of practical significance).

Note: The initialization of retrofit adds a line of code

Addcalladapterfactory (Rxjavacalladapterfactory.create ())
3, RxJava + Retrofit Advanced

In the above to save the user information after login example, in fact, when doing the project, often in the login after the user information is not obtained. The general login will get token, and then according to token to obtain the user's information. Their steps are this:

    • 1. Login

    • 2, access to user information (premise: Login success)

It can be seen that this is a nested structure ... Nested AH!!! My God, the most afraid of nesting structure.


Use Rxjava + retrofit to complete such a request

(using the example of a parabola, http://gank.io/post/560e15be2dca930e00da1083,

A little bit of change)

Interface method:

Call the Login method:

It is easy to complete a nested request with a flatmap (), and the logic is clear. So easy~~~

4, summary Rxjava practicality from the above two examples slowly manifested out, the more complex logic, Rxjava advantage is more obvious. Rxjava the use of the temporary introduction here, the use of the process encountered good to come out to share with you.

RxJava + Retrofit Complete Network request

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: info-contact@alibabacloud.com 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.