RxJava 2.0 tutorial from scratch (4) Application in Android

From: http://blog.csdn.net/qq_35064774/article/details/53065400

1. Preface

In article 123, I introduced the basic usage of RxJava 2.0. This article will be introduced in Android The basic usage of the phrase ___________.  
Attached are links to this series of tutorials.  
Rx Java 2.0 tutorial from scratch (1) Foundation 
RxJava 2.0 Tutorial (2) Operators from scratch 
RxJava 2.0 tutorial from scratch (3) The benefits of responsiveness 
RxJava 2.0 tutorial from scratch (4) Application in Android

2. RxAndroid

RxAndroid is RxJava for Android Extended Library of platform.

Once upon a time, RxAndroid did provide many practical methods, but then many people in the community had an opinion on the structure of the library, and the author reconstructed it. Now only Android Schedulers are retained. Now the basic RxAndroid has only one function, that is, the Android Schedulers. mainThread method is used to specify the main thread.

Previously those classes were classified into other Rx libraries, such as RxBinding, RxLifecycle. I'm not going to introduce it here, because it's too early for beginners. If you are interested, you can consult the relevant information by yourself.

3. Cooperate with Retrofit 2

For the moment, android The network library is basically dominated by Retrofit + OkHttp. Because it's really excellent.

Of course, the influence of RxJava can not be underestimated. Retrofit also provides support for RxJava. Up to now (2016.11.06), the official does not seem to support RxJava 2.0. But don't worry, God has already done it for us.

To use Retrofit, you need to introduce the corresponding libraries.

compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.10.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Retrofit2 relies on okhttp3, while okhttp3 relies on okio. The last one is the retrofit2-rxjava2-adapter adapter written by God.

Suppose we need to get the html code of Baidu Page now.

public interface BaiDuService {
    @GET("/")
    Flowable<ResponseBody> getText();
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Define the request interface. Note that the return value is of the Flowable type.

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://www.baidu.com/")
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())// Added adapter support for RxJava2
        .build();
BaiDuService service = retrofit.create(BaiDuService.class);
service.getText()
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<ResponseBody>() {
            @Override
            public void onSubscribe(Subscription s) {
                s.request(Long.MAX_VALUE);
            }

            @Override
            public void onNext(ResponseBody s) {
                Toast.makeText(RxJava2Activity.this, "Achieving Success", Toast.LENGTH_SHORT).show();
                try {
                    System.out.println(s.string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                Toast.makeText(RxJava2Activity.this, "Access failed, please check whether the network is free", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {
                System.out.println("End of Mission");
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

Add RxJava when creating Retrofit 2. The adapter so that the request can be returned directly to Flowable. Then you can do RxJava 2. Operation.

4. Getting data from RxJava 2

Earlier, I introduced how to send data such as arrays, lists and so on through fromX. So is there any way to get the transmitted data directly instead of exporting it through subscribers?

List<String> ids = Flowable.range(1, 100)
        .map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Exception {
                return "id:" + integer;
            }
        })
        .toList().blockingGet();
System.out.println(ids);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Here, for example, I've sent a total of 100 to 100 data, which we can get by blocking X.

5. End

This is the end of this series of tutorials, but I'm only introducing the tip of the iceberg for RxJava. If you have higher requirements, please refer to other materials.

Thank you for your reading.

Keywords: Retrofit Android network OkHttp

Added by leetcrew on Sun, 23 Jun 2019 00:17:57 +0300