├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── scut
│ │ └── carson_ho
│ │ └── rxjava_operators
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── scut
│ │ │ └── carson_ho
│ │ │ └── rxjava_operators
│ │ │ ├── CombineOperator
│ │ │ ├── UsageDemo1.java
│ │ │ ├── UsageDemo2.java
│ │ │ └── UsageDemo3.java
│ │ │ ├── EstablishOperator
│ │ │ ├── Demo
│ │ │ │ ├── GetRequest_Interface.java
│ │ │ │ ├── RxJavafixRxjava.java
│ │ │ │ └── Translation.java
│ │ │ └── establishUsage.java
│ │ │ ├── FilterUsage.java
│ │ │ ├── FunctionOperator
│ │ │ ├── RxJavafixRetrofit.java
│ │ │ ├── RxJavafixRetrofit1.java
│ │ │ └── RxJavafixRetrofit2.java
│ │ │ ├── MainActivity.java
│ │ │ └── SwitchOperator
│ │ │ ├── Demo
│ │ │ ├── GetRequest_Interface2.java
│ │ │ ├── RxJavaxRxtrofit3.java
│ │ │ ├── Translation1.java
│ │ │ └── Translation2.java
│ │ │ └── SwitchUsage.java
│ └── res
│ │ ├── layout
│ │ ├── activity_combine.xml
│ │ ├── activity_filter.xml
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── scut
│ └── carson_ho
│ └── rxjava_operators
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 |
36 |
37 |
38 |
39 |
40 | Android
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | 1.8
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RxJava_LearningMaterial
2 | 这是 一份全面介绍Android RxJava的学习资料,内容包括如下:
3 |
4 | 
5 |
6 | # 1. 入门教程
7 | [这是一篇 清晰 & 易懂的Rxjava 入门教程](http://blog.csdn.net/carson_ho/article/details/78179340)
8 |
9 | [Android :RxJava2.0到底更新了什么?(含使用建议)](http://blog.csdn.net/carson_ho/article/details/79157408)
10 |
11 | [这是一份面向初学者的RxJava使用指南](http://blog.csdn.net/carson_ho/article/details/79168602)
12 |
13 |
14 | # 2. 原理
15 |
16 | [手把手带你源码分析 RxJava](http://blog.csdn.net/carson_ho/article/details/79168523)
17 |
18 |
19 |
20 | # 3. 操作符详解 系列
21 | `RxJava`如此受欢迎的原因,在于其**提供了丰富 & 功能强大的操作符,几乎能完成所有的功能需求**
22 |
23 | [ RxJava操作符 详细使用手册](http://blog.csdn.net/carson_ho/article/details/79191327)
24 |
25 | [ 创建操作符](http://blog.csdn.net/carson_ho/article/details/78246732)
26 |
27 | [功能性操作符](http://blog.csdn.net/carson_ho/article/details/78537277)
28 |
29 | [过滤操作符](http://blog.csdn.net/carson_ho/article/details/78683064)
30 |
31 | [组合 / 合并操作符](http://blog.csdn.net/carson_ho/article/details/78455349)
32 |
33 | [ 变换操作符](http://blog.csdn.net/carson_ho/article/details/78315437)
34 |
35 | [条件 / 布尔操作符](http://blog.csdn.net/carson_ho/article/details/78949306)
36 |
37 | # 4. 实际应用场景(含开发实例讲解)
38 |
39 | [RxJava的最佳开发场景汇总](http://blog.csdn.net/carson_ho/article/details/79168723)
40 |
41 | [细说 线程控制(切换 / 调度 )](https://www.jianshu.com/p/5225b2baaecd)
42 |
43 | [一文带你全面了解 背压策略](http://blog.csdn.net/carson_ho/article/details/79081407)
44 |
45 | [网络请求嵌套回调](http://blog.csdn.net/carson_ho/article/details/78315696)
46 |
47 | [网络请求出错重连](http://blog.csdn.net/carson_ho/article/details/78651602)
48 |
49 | [网络请求轮询(有条件)](http://blog.csdn.net/carson_ho/article/details/78558790)
50 |
51 | [网络请求轮询(无条件)](http://blog.csdn.net/carson_ho/article/details/78256466)
52 |
53 | [联想搜索优化](http://blog.csdn.net/carson_ho/article/details/78849661)
54 |
55 | [功能防抖](http://blog.csdn.net/carson_ho/article/details/78849689)
56 |
57 | [从磁盘 / 内存缓存中 获取缓存数据](http://blog.csdn.net/carson_ho/article/details/78455449)
58 |
59 | [合并数据源 & 同时展示数据](http://blog.csdn.net/carson_ho/article/details/78455544)
60 |
61 | [联合判断](http://blog.csdn.net/carson_ho/article/details/78455624)
62 |
63 | # 5. 结合开源库使用:网络请求库Rerofit
64 |
65 | [Retrofit 与 RxJava联合使用汇总(含实例教程)](http://blog.csdn.net/carson_ho/article/details/79125101)
66 |
67 | [优雅实现 网络请求嵌套回调](http://blog.csdn.net/carson_ho/article/details/78315696)
68 |
69 | [网络请求轮询(有条件)](http://blog.csdn.net/carson_ho/article/details/78558790)
70 |
71 | [网络请求轮询(无条件)](http://blog.csdn.net/carson_ho/article/details/78256466)
72 |
73 | [网络请求出错重连](http://blog.csdn.net/carson_ho/article/details/78651602)
74 | [合并数据源 & 同时展示数据](http://blog.csdn.net/carson_ho/article/details/78455544)
75 |
76 | # 关于作者
77 | - ID:Carson_Ho
78 | - 简介:CSDN签约作者、简书推荐作者、稀土掘金专栏作者
79 | - E - mail:carson.ho@foxmail.com
80 | - Github:https://github.com/Carson-Ho
81 | - CSDN:http://blog.csdn.net/carson_ho
82 | - 简书:http://www.jianshu.com/u/383970bef0a0
83 | - 稀土掘金:https://juejin.im/user/58d4d9781b69e6006ba65edc
84 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "scut.carson_ho.rxjava_operators"
8 | minSdkVersion 19
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.3.1'
28 | compile 'com.android.support.constraint:constraint-layout:1.0.2'
29 | // compile 'io.reactivex:rxandroid:1.2.0'
30 | // compile 'io.reactivex:rxjava:1.1.5'
31 | compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
32 | compile 'io.reactivex.rxjava2:rxjava:2.0.7'
33 |
34 | compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
35 |
36 | compile 'com.squareup.retrofit2:retrofit:2.1.0'
37 | compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
38 | compile 'com.squareup.retrofit2:converter-gson:2.1.0'
39 | testCompile 'junit:junit:4.12'
40 | }
41 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/Carson_Ho/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/scut/carson_ho/rxjava_operators/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("scut.carson_ho.rxjava_operators", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/CombineOperator/UsageDemo1.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.CombineOperator;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import io.reactivex.Observable;
8 | import io.reactivex.ObservableEmitter;
9 | import io.reactivex.ObservableOnSubscribe;
10 | import io.reactivex.functions.Consumer;
11 | import scut.carson_ho.rxjava_operators.R;
12 |
13 | /**
14 | * Created by Carson_Ho on 17/9/12.
15 | * 从磁盘 / 内存缓存中 获取缓存数据
16 | */
17 |
18 | public class UsageDemo1 extends AppCompatActivity {
19 | // 该2变量用于模拟内存缓存 & 磁盘缓存中的数据
20 | String memoryCache = null;
21 | String diskCache = "从磁盘缓存中获取数据";
22 |
23 | private String TAG = "RxJava";
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_combine);
29 |
30 |
31 | /*
32 | * 设置第1个Observable:检查内存缓存是否有该数据的缓存
33 | **/
34 | Observable memory = Observable.create(new ObservableOnSubscribe() {
35 | @Override
36 | public void subscribe(ObservableEmitter emitter) throws Exception {
37 |
38 | // 先判断内存缓存有无数据
39 | if (memoryCache != null) {
40 | // 若有该数据,则发送
41 | emitter.onNext(memoryCache);
42 | } else {
43 | // 若无该数据,则直接发送结束事件
44 | emitter.onComplete();
45 | }
46 |
47 | }
48 | });
49 |
50 | /*
51 | * 设置第2个Observable:检查磁盘缓存是否有该数据的缓存
52 | **/
53 | Observable disk = Observable.create(new ObservableOnSubscribe() {
54 | @Override
55 | public void subscribe(ObservableEmitter emitter) throws Exception {
56 |
57 | // 先判断磁盘缓存有无数据
58 | if (diskCache != null) {
59 | // 若有该数据,则发送
60 | emitter.onNext(diskCache);
61 | } else {
62 | // 若无该数据,则直接发送结束事件
63 | emitter.onComplete();
64 | }
65 |
66 | }
67 | });
68 |
69 | /*
70 | * 设置第3个Observable:通过网络获取数据
71 | **/
72 | Observable network = Observable.just("从网络中获取数据");
73 | // 此处仅作网络请求的模拟
74 |
75 |
76 | /*
77 | * 通过concat() 和 firstElement()操作符实现缓存功能
78 | **/
79 |
80 | // 1. 通过concat()合并memory、disk、network 3个被观察者的事件(即检查内存缓存、磁盘缓存 & 发送网络请求)
81 | // 并将它们按顺序串联成队列
82 | Observable.concat(memory, disk, network)
83 | // 2. 通过firstElement(),从串联队列中取出并发送第1个有效事件(Next事件),即依次判断检查memory、disk、network
84 | .firstElement()
85 | // 即本例的逻辑为:
86 | // a. firstElement()取出第1个事件 = memory,即先判断内存缓存中有无数据缓存;由于memoryCache = null,即内存缓存中无数据,所以发送结束事件(视为无效事件)
87 | // b. firstElement()继续取出第2个事件 = disk,即判断磁盘缓存中有无数据缓存:由于diskCache ≠ null,即磁盘缓存中有数据,所以发送Next事件(有效事件)
88 | // c. 即firstElement()已发出第1个有效事件(disk事件),所以停止判断。
89 |
90 | // 3. 观察者订阅
91 | .subscribe(new Consumer() {
92 | @Override
93 | public void accept( String s) throws Exception {
94 | Log.d(TAG,"最终获取的数据来源 = "+ s);
95 | }
96 | });
97 | }
98 | }
99 |
100 |
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/CombineOperator/UsageDemo2.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.CombineOperator;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import io.reactivex.Observable;
8 | import io.reactivex.Observer;
9 | import io.reactivex.disposables.Disposable;
10 | import scut.carson_ho.rxjava_operators.R;
11 |
12 | /**
13 | * Created by Carson_Ho on 17/9/12.
14 | * 合并数据源 & 统一展示
15 | */
16 |
17 | public class UsageDemo2 extends AppCompatActivity {
18 |
19 | private String TAG = "RxJava";
20 | // 用于存放最终展示的数据
21 | private String result = "数据源来自 = " ;
22 |
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_combine);
28 |
29 | /*
30 | * 设置第1个Observable:通过网络获取数据
31 | * 此处仅作网络请求的模拟
32 | **/
33 | Observable network = Observable.just("网络");
34 |
35 | /*
36 | * 设置第2个Observable:通过本地文件获取数据
37 | * 此处仅作本地文件请求的模拟
38 | **/
39 | Observable file = Observable.just("本地文件");
40 |
41 | /*
42 | * 通过merge()合并事件 & 同时发送事件
43 | **/
44 | Observable.merge(network, file)
45 | .subscribe(new Observer() {
46 | @Override
47 | public void onSubscribe(Disposable d) {
48 |
49 | }
50 |
51 | @Override
52 | public void onNext(String value) {
53 | Log.d(TAG, "数据源有: "+ value );
54 | result += value + "+";
55 | }
56 |
57 | @Override
58 | public void onError(Throwable e) {
59 | Log.d(TAG, "对Error事件作出响应");
60 | }
61 |
62 | // 接收合并事件后,统一展示
63 | @Override
64 | public void onComplete() {
65 | Log.d(TAG, "获取数据完成");
66 | Log.d(TAG, result );
67 | }
68 | });
69 | }
70 | }
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/CombineOperator/UsageDemo3.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.CombineOperator;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.text.TextUtils;
6 | import android.util.Log;
7 | import android.widget.Button;
8 | import android.widget.EditText;
9 |
10 | import com.jakewharton.rxbinding2.widget.RxTextView;
11 |
12 | import io.reactivex.Observable;
13 | import io.reactivex.annotations.NonNull;
14 | import io.reactivex.functions.Consumer;
15 | import io.reactivex.functions.Function3;
16 | import scut.carson_ho.rxjava_operators.R;
17 |
18 | /**
19 | * Created by Carson_Ho on 17/9/12.
20 | * 联合判断
21 | */
22 |
23 | public class UsageDemo3 extends AppCompatActivity {
24 |
25 | private String TAG = "RxJava";
26 |
27 |
28 | /*
29 | * 步骤1:设置控件变量 & 绑定
30 | **/
31 | EditText name,age,job;
32 | Button list;
33 |
34 |
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.activity_combine);
40 |
41 |
42 | name = (EditText) findViewById(R.id.name);
43 | age = (EditText) findViewById(R.id.age);
44 | job = (EditText) findViewById(R.id.job);
45 | list = (Button) findViewById(R.id.list);
46 |
47 | /*
48 | * 步骤2:为每个EditText设置被观察者,用于发送监听事件
49 | * 说明:
50 | * 1. 此处采用了RxBinding,需要引入依赖:compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
51 | * 2. 传入EditText控件,点击任1个EditText撰写时,都会发送数据事件 = Function3()的返回值(下面会详细说明)
52 | * 3. 采用skip(1)原因:跳过 一开始EditText无任何输入时的空值
53 | **/
54 | Observable nameObservable = RxTextView.textChanges(name).skip(1);
55 | Observable ageObservable = RxTextView.textChanges(age).skip(1);
56 | Observable jobObservable = RxTextView.textChanges(job).skip(1);
57 |
58 | /*
59 | * 步骤3:通过combineLatest()合并事件 & 联合判断
60 | **/
61 | Observable.combineLatest(nameObservable,ageObservable,jobObservable,new Function3() {
62 | @Override
63 | public Boolean apply(@NonNull CharSequence charSequence, @NonNull CharSequence charSequence2, @NonNull CharSequence charSequence3) throws Exception {
64 |
65 | /*
66 | * 步骤4:规定表单信息输入不能为空
67 | **/
68 | // 1. 姓名信息
69 | boolean isUserNameValid = !TextUtils.isEmpty(name.getText()) ;
70 | // 除了设置为空,也可设置长度限制
71 | // boolean isUserNameValid = !TextUtils.isEmpty(name.getText()) && (name.getText().toString().length() > 2 && name.getText().toString().length() < 9);
72 |
73 | // 2. 年龄信息
74 | boolean isUserAgeValid = !TextUtils.isEmpty(age.getText());
75 | // 3. 职业信息
76 | boolean isUserJobValid = !TextUtils.isEmpty(job.getText()) ;
77 |
78 | /*
79 | * 步骤5:返回信息 = 联合判断,即3个信息同时已填写,"提交按钮"才可点击
80 | **/
81 | return isUserNameValid && isUserAgeValid && isUserJobValid;
82 | }
83 |
84 | }).subscribe(new Consumer() {
85 | @Override
86 | public void accept(Boolean s) throws Exception {
87 | /*
88 | * 步骤6:返回结果 & 设置按钮可点击样式
89 | **/
90 | Log.e(TAG, "提交按钮是否可点击: "+s);
91 | list.setEnabled(s);
92 | }
93 | });
94 |
95 |
96 |
97 |
98 |
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/EstablishOperator/Demo/GetRequest_Interface.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.EstablishOperator.Demo;
2 |
3 | import io.reactivex.Observable;
4 | import retrofit2.http.GET;
5 |
6 | /**
7 | * Created by Carson_Ho on 17/9/8.
8 | */
9 |
10 | public interface GetRequest_Interface {
11 |
12 | @GET("ajax.php?a=fy&f=auto&t=auto&w=hi%20world")
13 | Observable getCall();
14 | // 注解里传入 网络请求 的部分URL地址
15 | // Retrofit把网络请求的URL分成了两部分:一部分放在Retrofit对象里,另一部分放在网络请求接口里
16 | // 如果接口里的url是一个完整的网址,那么放在Retrofit对象里的URL可以忽略
17 | // 采用Observable<...>接口
18 | // getCall()是接受网络请求数据的方法
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/EstablishOperator/Demo/RxJavafixRxjava.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.EstablishOperator.Demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
8 |
9 | import java.util.concurrent.TimeUnit;
10 |
11 | import io.reactivex.Observable;
12 | import io.reactivex.Observer;
13 | import io.reactivex.android.schedulers.AndroidSchedulers;
14 | import io.reactivex.disposables.Disposable;
15 | import io.reactivex.functions.Consumer;
16 | import io.reactivex.schedulers.Schedulers;
17 | import retrofit2.Retrofit;
18 | import retrofit2.converter.gson.GsonConverterFactory;
19 | import scut.carson_ho.rxjava_operators.R;
20 |
21 | /**
22 | * Created by Carson_Ho on 17/9/8.
23 | * 实战系列:无条件轮询
24 | */
25 |
26 | public class RxJavafixRxjava extends AppCompatActivity {
27 |
28 | private static final String TAG = "Rxjava";
29 |
30 | @Override
31 | protected void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_main);
34 |
35 | /*
36 | * 步骤1:采用interval()延迟发送
37 | **/
38 | Observable.interval(2,1, TimeUnit.SECONDS)
39 | // 参数说明:
40 | // 参数1 = 第1次延迟时间;
41 | // 参数2 = 间隔时间数字;
42 | // 参数3 = 时间单位;
43 | // 该例子发送的事件特点:延迟2s后发送事件,每隔1秒产生1个数字(从0开始递增1,无限个)
44 |
45 | /*
46 | * 步骤2:每次发送数字前发送1次网络请求(doOnNext()在执行Next事件前调用)
47 | * 即每隔1秒产生1个数字前,就发送1次网络请求,从而实现轮询需求
48 | **/
49 | .doOnNext(new Consumer() {
50 | @Override
51 | public void accept(Long integer) throws Exception {
52 | Log.d(TAG, "第 " + integer + " 次轮询" );
53 |
54 | /*
55 | * 步骤3:通过Retrofit发送网络请求
56 | **/
57 | // a. 创建Retrofit对象
58 | Retrofit retrofit = new Retrofit.Builder()
59 | .baseUrl("http://fy.iciba.com/") // 设置 网络请求 Url
60 | .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
61 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 支持RxJava
62 | .build();
63 |
64 | // b. 创建 网络请求接口 的实例
65 | GetRequest_Interface request = retrofit.create(GetRequest_Interface.class);
66 |
67 | // c. 采用Observable<...>形式 对 网络请求 进行封装
68 | Observable observable = request.getCall();
69 | // d. 通过线程切换发送网络请求
70 | observable.subscribeOn(Schedulers.io()) // 切换到IO线程进行网络请求
71 | .observeOn(AndroidSchedulers.mainThread()) // 切换回到主线程 处理请求结果
72 | .subscribe(new Observer() {
73 | @Override
74 | public void onSubscribe(Disposable d) {
75 | }
76 |
77 | @Override
78 | public void onNext(Translation result) {
79 | // e.接收服务器返回的数据
80 | result.show() ;
81 | }
82 |
83 | @Override
84 | public void onError(Throwable e) {
85 | Log.d(TAG, "请求失败");
86 | }
87 |
88 | @Override
89 | public void onComplete() {
90 |
91 | }
92 | });
93 |
94 | }
95 | }).subscribe(new Observer() {
96 | @Override
97 | public void onSubscribe(Disposable d) {
98 |
99 | }
100 | @Override
101 | public void onNext(Long value) {
102 |
103 | }
104 |
105 | @Override
106 | public void onError(Throwable e) {
107 | Log.d(TAG, "对Error事件作出响应");
108 | }
109 |
110 | @Override
111 | public void onComplete() {
112 | Log.d(TAG, "对Complete事件作出响应");
113 | }
114 | });
115 |
116 |
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/EstablishOperator/Demo/Translation.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.EstablishOperator.Demo;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * Created by Carson_Ho on 17/9/8.
7 | */
8 |
9 | public class Translation {
10 |
11 |
12 | private int status;
13 |
14 | private content content;
15 | private static class content {
16 | private String from;
17 | private String to;
18 | private String vendor;
19 | private String out;
20 | private int errNo;
21 | }
22 |
23 | //定义 输出返回数据 的方法
24 | public void show() {
25 | Log.d("RxJava", content.out );
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/scut/carson_ho/rxjava_operators/EstablishOperator/establishUsage.java:
--------------------------------------------------------------------------------
1 | package scut.carson_ho.rxjava_operators.EstablishOperator;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 | import java.util.concurrent.TimeUnit;
10 |
11 | import io.reactivex.Observable;
12 | import io.reactivex.ObservableEmitter;
13 | import io.reactivex.ObservableOnSubscribe;
14 | import io.reactivex.ObservableSource;
15 | import io.reactivex.Observer;
16 | import io.reactivex.annotations.NonNull;
17 | import io.reactivex.disposables.Disposable;
18 | import io.reactivex.functions.Function;
19 | import scut.carson_ho.rxjava_operators.R;
20 |
21 | /**
22 | * Created by Carson_Ho on 17/9/6.
23 | * 应用场景 & 对应操作符介绍
24 | */
25 |
26 | public class establishUsage extends AppCompatActivity {
27 |
28 | private String TAG = "RxJava";
29 | Integer i = 10;
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_main);
35 |
36 |
37 |
38 | Observable.just(1,2,4).repeatWhen(new Function, ObservableSource>>() {
39 | @Override
40 | public ObservableSource> apply(@NonNull Observable