├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── zlc
│ │ │ │ └── season
│ │ │ │ └── rxjava2demo
│ │ │ │ ├── entity
│ │ │ │ ├── LoginRequest.java
│ │ │ │ ├── LoginResponse.java
│ │ │ │ ├── RegisterRequest.java
│ │ │ │ ├── RegisterResponse.java
│ │ │ │ ├── UserBaseInfoRequest.java
│ │ │ │ ├── UserBaseInfoResponse.java
│ │ │ │ ├── UserExtraInfoRequest.java
│ │ │ │ ├── UserExtraInfoResponse.java
│ │ │ │ └── UserInfo.java
│ │ │ │ ├── RetrofitProvider.java
│ │ │ │ ├── Api.java
│ │ │ │ ├── demo
│ │ │ │ ├── RetrofitDemo.java
│ │ │ │ ├── ChapterFive.java
│ │ │ │ ├── ChapterOne.java
│ │ │ │ ├── ChapterTwo.java
│ │ │ │ ├── ChapterThree.java
│ │ │ │ ├── ChapterSix.java
│ │ │ │ ├── ChapterSeven.java
│ │ │ │ ├── ChapterFour.java
│ │ │ │ ├── ChapterNine.java
│ │ │ │ └── ChapterEight.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── zlc
│ │ │ └── season
│ │ │ └── rxjava2demo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── zlc
│ │ └── season
│ │ └── rxjava2demo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.md
├── test.txt
├── gradle.properties
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RxJava2Demo
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ssseasonnn/RxJava2Demo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | .idea
4 | /local.properties
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ssseasonnn/RxJava2Demo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ssseasonnn/RxJava2Demo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ssseasonnn/RxJava2Demo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ssseasonnn/RxJava2Demo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ssseasonnn/RxJava2Demo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RxJava2Demo
2 | 给初学者的RxJava2.0系列教程的demo
3 |
4 | 教程地址:
5 |
6 | [简书](http://www.jianshu.com/p/464fa025229e)
7 |
8 | [掘金](https://gold.xitu.io/post/5848d96761ff4b0058c9d3dc)
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/LoginRequest.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/6
6 | * Time: 11:31
7 | * FIXME
8 | */
9 | public class LoginRequest {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/LoginResponse.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/6
6 | * Time: 11:31
7 | * FIXME
8 | */
9 | public class LoginResponse {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/RegisterRequest.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/6
6 | * Time: 11:31
7 | * FIXME
8 | */
9 | public class RegisterRequest {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/RegisterResponse.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/6
6 | * Time: 11:31
7 | * FIXME
8 | */
9 | public class RegisterResponse {
10 | }
11 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/UserBaseInfoRequest.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/9
6 | * Time: 16:10
7 | * FIXME
8 | */
9 | public class UserBaseInfoRequest {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/UserBaseInfoResponse.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/9
6 | * Time: 16:10
7 | * FIXME
8 | */
9 | public class UserBaseInfoResponse {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/UserExtraInfoRequest.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/9
6 | * Time: 16:11
7 | * FIXME
8 | */
9 | public class UserExtraInfoRequest {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/UserExtraInfoResponse.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/9
6 | * Time: 16:11
7 | * FIXME
8 | */
9 | public class UserExtraInfoResponse {
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/test.txt:
--------------------------------------------------------------------------------
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 | 不带走一片云彩。
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/entity/UserInfo.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.entity;
2 |
3 | /**
4 | * Author: Season(ssseasonnn@gmail.com)
5 | * Date: 2016/12/9
6 | * Time: 16:13
7 | * FIXME
8 | */
9 | public class UserInfo {
10 | UserBaseInfoResponse mBaseInfo;
11 | UserExtraInfoResponse mExtraInfo;
12 |
13 | public UserInfo(UserBaseInfoResponse baseInfo, UserExtraInfoResponse extraInfo) {
14 | mBaseInfo = baseInfo;
15 | mExtraInfo = extraInfo;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/test/java/zlc/season/rxjava2demo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/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 D:\Application\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 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Fri Dec 02 10:32:58 CST 2016
16 | systemProp.http.proxyHost=127.0.0.1
17 | org.gradle.jvmargs=-Xmx1536m
18 | systemProp.http.proxyPort=1080
19 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/zlc/season/rxjava2demo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo;
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("zlc.season.rxjava2demo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/RetrofitProvider.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo;
2 |
3 | import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import okhttp3.OkHttpClient;
8 | import retrofit2.Retrofit;
9 | import retrofit2.converter.gson.GsonConverterFactory;
10 |
11 | /**
12 | * Author: Season(ssseasonnn@gmail.com)
13 | * Date: 2016/12/9
14 | * Time: 16:46
15 | * FIXME
16 | */
17 | public class RetrofitProvider {
18 | private static final String ENDPOINT = "https://api.douban.com/";
19 |
20 | public static Retrofit get() {
21 | OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
22 | builder.readTimeout(10, TimeUnit.SECONDS);
23 | builder.connectTimeout(9, TimeUnit.SECONDS);
24 |
25 | return new Retrofit.Builder().baseUrl(ENDPOINT)
26 | .client(builder.build())
27 | .addConverterFactory(GsonConverterFactory.create())
28 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
29 | .build();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
22 |
23 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.1"
6 | defaultConfig {
7 | applicationId "zlc.season.rxjava2demo"
8 | minSdkVersion 11
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(include: ['*.jar'], dir: 'libs')
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.0.1'
28 | testCompile 'junit:junit:4.12'
29 | compile 'io.reactivex.rxjava2:rxjava:2.0.1'
30 | compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
31 | //retrofit
32 | compile 'com.squareup.retrofit2:retrofit:2.1.0'
33 | compile 'com.squareup.retrofit2:converter-gson:2.1.0'
34 | compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
35 | compile 'com.squareup.okhttp3:okhttp:3.4.1'
36 | compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/Api.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo;
2 |
3 | import io.reactivex.Observable;
4 | import okhttp3.ResponseBody;
5 | import retrofit2.Response;
6 | import retrofit2.http.Body;
7 | import retrofit2.http.GET;
8 | import retrofit2.http.Query;
9 | import zlc.season.rxjava2demo.entity.LoginRequest;
10 | import zlc.season.rxjava2demo.entity.LoginResponse;
11 | import zlc.season.rxjava2demo.entity.RegisterRequest;
12 | import zlc.season.rxjava2demo.entity.RegisterResponse;
13 | import zlc.season.rxjava2demo.entity.UserBaseInfoRequest;
14 | import zlc.season.rxjava2demo.entity.UserBaseInfoResponse;
15 | import zlc.season.rxjava2demo.entity.UserExtraInfoRequest;
16 | import zlc.season.rxjava2demo.entity.UserExtraInfoResponse;
17 |
18 | /**
19 | * Author: Season(ssseasonnn@gmail.com)
20 | * Date: 2016/12/6
21 | * Time: 11:30
22 | * FIXME
23 | */
24 | public interface Api {
25 | @GET
26 | Observable login(@Body LoginRequest request);
27 |
28 | @GET
29 | Observable register(@Body RegisterRequest request);
30 |
31 | @GET
32 | Observable getUserBaseInfo(@Body UserBaseInfoRequest request);
33 |
34 | @GET
35 | Observable getUserExtraInfo(@Body UserExtraInfoRequest request);
36 |
37 | @GET("v2/movie/top250")
38 | Observable> getTop250(@Query("start") int start, @Query("count") int count);
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/RetrofitDemo.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import io.reactivex.android.schedulers.AndroidSchedulers;
7 | import io.reactivex.functions.Consumer;
8 | import io.reactivex.schedulers.Schedulers;
9 | import okhttp3.ResponseBody;
10 | import retrofit2.Response;
11 | import zlc.season.rxjava2demo.Api;
12 | import zlc.season.rxjava2demo.RetrofitProvider;
13 |
14 | import static zlc.season.rxjava2demo.MainActivity.TAG;
15 |
16 | /**
17 | * Author: Season(ssseasonnn@gmail.com)
18 | * Date: 2016/12/14
19 | * Time: 11:08
20 | * FIXME
21 | */
22 | public class RetrofitDemo {
23 | public static void demo1(final Context context) {
24 | final Api api = RetrofitProvider.get().create(Api.class);
25 | api.getTop250(0, 10)
26 | .subscribeOn(Schedulers.io())
27 | .observeOn(AndroidSchedulers.mainThread())
28 | .subscribe(new Consumer>() {
29 | @Override
30 | public void accept(Response response) throws Exception {
31 | Log.d(TAG, response.body().string());
32 | }
33 | }, new Consumer() {
34 | @Override
35 | public void accept(Throwable throwable) throws Exception {
36 | Log.w("Error", throwable);
37 | }
38 | });
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 | import android.view.View;
7 |
8 | import java.io.InterruptedIOException;
9 |
10 | import io.reactivex.functions.Consumer;
11 | import io.reactivex.plugins.RxJavaPlugins;
12 | import zlc.season.rxjava2demo.demo.ChapterNine;
13 |
14 | public class MainActivity extends AppCompatActivity {
15 | public static final String TAG = "TAG";
16 |
17 | static {
18 | RxJavaPlugins.setErrorHandler(new Consumer() {
19 | @Override
20 | public void accept(Throwable throwable) throws Exception {
21 | if (throwable instanceof InterruptedIOException) {
22 | Log.d(TAG, "Io interrupted");
23 | }
24 | }
25 | });
26 | }
27 |
28 | @Override
29 | protected void onCreate(Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 | setContentView(R.layout.activity_main);
32 |
33 | findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
34 | @Override
35 | public void onClick(View view) {
36 | ChapterNine.demo4();
37 | }
38 | });
39 |
40 | findViewById(R.id.request).setOnClickListener(new View.OnClickListener() {
41 | @Override
42 | public void onClick(View v) {
43 | ChapterNine.request(96);
44 | }
45 | });
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterFive.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import io.reactivex.Observable;
6 | import io.reactivex.ObservableEmitter;
7 | import io.reactivex.ObservableOnSubscribe;
8 | import io.reactivex.android.schedulers.AndroidSchedulers;
9 | import io.reactivex.functions.BiFunction;
10 | import io.reactivex.functions.Consumer;
11 | import io.reactivex.schedulers.Schedulers;
12 |
13 | import static zlc.season.rxjava2demo.MainActivity.TAG;
14 |
15 | /**
16 | * Author: Season(ssseasonnn@gmail.com)
17 | * Date: 2016/12/12
18 | * Time: 10:59
19 | * FIXME
20 | */
21 | public class ChapterFive {
22 | public static void demo1() {
23 | Observable observable1 = Observable.create(new ObservableOnSubscribe() {
24 | @Override
25 | public void subscribe(ObservableEmitter emitter) throws Exception {
26 | for (int i = 0; ; i++) {
27 | emitter.onNext(i);
28 | }
29 | }
30 | }).subscribeOn(Schedulers.io());
31 |
32 | Observable observable2 = Observable.create(new ObservableOnSubscribe() {
33 | @Override
34 | public void subscribe(ObservableEmitter emitter) throws Exception {
35 | emitter.onNext("A");
36 | }
37 | }).subscribeOn(Schedulers.io());
38 |
39 | Observable.zip(observable1, observable2, new BiFunction() {
40 | @Override
41 | public String apply(Integer integer, String s) throws Exception {
42 | return integer + s;
43 | }
44 | }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
45 | @Override
46 | public void accept(String s) throws Exception {
47 | Log.d(TAG, s);
48 | }
49 | }, new Consumer() {
50 | @Override
51 | public void accept(Throwable throwable) throws Exception {
52 | Log.w(TAG, throwable);
53 | }
54 | });
55 | }
56 |
57 | public static void demo2() {
58 | Observable.create(new ObservableOnSubscribe() {
59 | @Override
60 | public void subscribe(ObservableEmitter emitter) throws Exception {
61 | for (int i = 0; ; i++) {
62 | emitter.onNext(i);
63 | }
64 | }
65 | }).subscribe(new Consumer() {
66 | @Override
67 | public void accept(Integer integer) throws Exception {
68 | Thread.sleep(2000);
69 | Log.d(TAG, "" + integer);
70 | }
71 | });
72 | }
73 |
74 | public static void demo3() {
75 | Observable.create(new ObservableOnSubscribe() {
76 | @Override
77 | public void subscribe(ObservableEmitter emitter) throws Exception {
78 | for (int i = 0; ; i++) {
79 | emitter.onNext(i);
80 | }
81 | }
82 | }).subscribeOn(Schedulers.io())
83 | .observeOn(AndroidSchedulers.mainThread())
84 | .subscribe(new Consumer() {
85 | @Override
86 | public void accept(Integer integer) throws Exception {
87 | Log.d(TAG, "" + integer);
88 | }
89 | });
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterOne.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import io.reactivex.Observable;
6 | import io.reactivex.ObservableEmitter;
7 | import io.reactivex.ObservableOnSubscribe;
8 | import io.reactivex.Observer;
9 | import io.reactivex.disposables.Disposable;
10 | import io.reactivex.functions.Consumer;
11 |
12 | import static zlc.season.rxjava2demo.MainActivity.TAG;
13 |
14 | /**
15 | * Author: Season(ssseasonnn@gmail.com)
16 | * Date: 2016/12/9
17 | * Time: 16:44
18 | * FIXME
19 | */
20 | public class ChapterOne {
21 | public static void demo1() {
22 | //创建一个上游 Observable:
23 | Observable observable = Observable.create(new ObservableOnSubscribe() {
24 | @Override
25 | public void subscribe(ObservableEmitter emitter) throws Exception {
26 | emitter.onNext(1);
27 | emitter.onNext(2);
28 | emitter.onNext(3);
29 | emitter.onComplete();
30 | }
31 | });
32 | //创建一个下游 Observer
33 | Observer observer = new Observer() {
34 | @Override
35 | public void onSubscribe(Disposable d) {
36 | Log.d(TAG, "subscribe");
37 | }
38 |
39 | @Override
40 | public void onNext(Integer value) {
41 | Log.d(TAG, "" + value);
42 | }
43 |
44 | @Override
45 | public void onError(Throwable e) {
46 | Log.d(TAG, "error");
47 | }
48 |
49 | @Override
50 | public void onComplete() {
51 | Log.d(TAG, "complete");
52 | }
53 | };
54 | //建立连接
55 | observable.subscribe(observer);
56 | }
57 |
58 | public static void demo2() {
59 | Observable.create(new ObservableOnSubscribe() {
60 | @Override
61 | public void subscribe(ObservableEmitter emitter) throws Exception {
62 | emitter.onNext(1);
63 | emitter.onNext(2);
64 | emitter.onNext(3);
65 | emitter.onComplete();
66 | }
67 | }).subscribe(new Observer() {
68 | @Override
69 | public void onSubscribe(Disposable d) {
70 | Log.d(TAG, "subscribe");
71 | }
72 |
73 | @Override
74 | public void onNext(Integer value) {
75 | Log.d(TAG, "" + value);
76 | }
77 |
78 | @Override
79 | public void onError(Throwable e) {
80 | Log.d(TAG, "error");
81 | }
82 |
83 | @Override
84 | public void onComplete() {
85 | Log.d(TAG, "complete");
86 | }
87 | });
88 | }
89 |
90 | public static void demo3() {
91 | Observable.create(new ObservableOnSubscribe() {
92 | @Override
93 | public void subscribe(ObservableEmitter emitter) throws Exception {
94 | Log.d(TAG, "emit 1");
95 | emitter.onNext(1);
96 | Log.d(TAG, "emit 2");
97 | emitter.onNext(2);
98 | Log.d(TAG, "emit 3");
99 | emitter.onNext(3);
100 | Log.d(TAG, "emit complete");
101 | emitter.onComplete();
102 | Log.d(TAG, "emit 4");
103 | emitter.onNext(4);
104 | }
105 | }).subscribe(new Observer() {
106 | private Disposable mDisposable;
107 | private int i;
108 |
109 | @Override
110 | public void onSubscribe(Disposable d) {
111 | Log.d(TAG, "subscribe");
112 | mDisposable = d;
113 | }
114 |
115 | @Override
116 | public void onNext(Integer value) {
117 | Log.d(TAG, "onNext: " + value);
118 | i++;
119 | if (i == 2) {
120 | Log.d(TAG, "dispose");
121 | mDisposable.dispose();
122 | Log.d(TAG, "isDisposed : " + mDisposable.isDisposed());
123 | }
124 | }
125 |
126 | @Override
127 | public void onError(Throwable e) {
128 | Log.d(TAG, "error");
129 | }
130 |
131 | @Override
132 | public void onComplete() {
133 | Log.d(TAG, "complete");
134 | }
135 | });
136 | }
137 |
138 | public static void demo4() {
139 | Observable.create(new ObservableOnSubscribe() {
140 | @Override
141 | public void subscribe(ObservableEmitter emitter) throws Exception {
142 | Log.d(TAG, "emit 1");
143 | emitter.onNext(1);
144 | Log.d(TAG, "emit 2");
145 | emitter.onNext(2);
146 | Log.d(TAG, "emit 3");
147 | emitter.onNext(3);
148 | Log.d(TAG, "emit complete");
149 | emitter.onComplete();
150 | Log.d(TAG, "emit 4");
151 | emitter.onNext(4);
152 | }
153 | }).subscribe(new Consumer() {
154 | @Override
155 | public void accept(Integer integer) throws Exception {
156 | Log.d(TAG, "onNext: " + integer);
157 | }
158 | });
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterTwo.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 | import android.widget.Toast;
6 |
7 | import io.reactivex.Observable;
8 | import io.reactivex.ObservableEmitter;
9 | import io.reactivex.ObservableOnSubscribe;
10 | import io.reactivex.Observer;
11 | import io.reactivex.android.schedulers.AndroidSchedulers;
12 | import io.reactivex.disposables.Disposable;
13 | import io.reactivex.functions.Consumer;
14 | import io.reactivex.schedulers.Schedulers;
15 | import zlc.season.rxjava2demo.Api;
16 | import zlc.season.rxjava2demo.RetrofitProvider;
17 | import zlc.season.rxjava2demo.entity.LoginRequest;
18 | import zlc.season.rxjava2demo.entity.LoginResponse;
19 |
20 | import static zlc.season.rxjava2demo.MainActivity.TAG;
21 |
22 | /**
23 | * Author: Season(ssseasonnn@gmail.com)
24 | * Date: 2016/12/9
25 | * Time: 16:48
26 | * FIXME
27 | */
28 | public class ChapterTwo {
29 | public static void demo1() {
30 | Observable observable = Observable.create(new ObservableOnSubscribe() {
31 | @Override
32 | public void subscribe(ObservableEmitter emitter) throws Exception {
33 | Log.d(TAG, "Observable thread is : " + Thread.currentThread().getName());
34 | Log.d(TAG, "emit 1");
35 | emitter.onNext(1);
36 | }
37 | });
38 |
39 | Consumer consumer = new Consumer() {
40 | @Override
41 | public void accept(Integer integer) throws Exception {
42 | Log.d(TAG, "Observer thread is :" + Thread.currentThread().getName());
43 | Log.d(TAG, "onNext: " + integer);
44 | }
45 | };
46 |
47 | observable.subscribe(consumer);
48 | }
49 |
50 | public static void demo2() {
51 | Observable observable = Observable.create(new ObservableOnSubscribe() {
52 | @Override
53 | public void subscribe(ObservableEmitter emitter) throws Exception {
54 | Log.d(TAG, "Observable thread is : " + Thread.currentThread().getName());
55 | Log.d(TAG, "emit 1");
56 | emitter.onNext(1);
57 | }
58 | });
59 |
60 | Consumer consumer = new Consumer() {
61 | @Override
62 | public void accept(Integer integer) throws Exception {
63 | Log.d(TAG, "Observer thread is :" + Thread.currentThread().getName());
64 | Log.d(TAG, "onNext: " + integer);
65 | }
66 | };
67 |
68 | observable.subscribeOn(Schedulers.newThread())
69 | .observeOn(AndroidSchedulers.mainThread())
70 | .subscribe(consumer);
71 | }
72 |
73 | public static void demo3() {
74 | Observable observable = Observable.create(new ObservableOnSubscribe() {
75 | @Override
76 | public void subscribe(ObservableEmitter emitter) throws Exception {
77 | Log.d(TAG, "Observable thread is : " + Thread.currentThread().getName());
78 | Log.d(TAG, "emit 1");
79 | emitter.onNext(1);
80 | }
81 | });
82 |
83 | Consumer consumer = new Consumer() {
84 | @Override
85 | public void accept(Integer integer) throws Exception {
86 | Log.d(TAG, "Observer thread is :" + Thread.currentThread().getName());
87 | Log.d(TAG, "onNext: " + integer);
88 | }
89 | };
90 |
91 | observable.subscribeOn(Schedulers.newThread())
92 | .subscribeOn(Schedulers.io())
93 | .observeOn(AndroidSchedulers.mainThread())
94 | .doOnNext(new Consumer() {
95 | @Override
96 | public void accept(Integer integer) throws Exception {
97 | Log.d(TAG, "After observeOn(mainThread), current thread is: " + Thread.currentThread()
98 | .getName());
99 | }
100 | })
101 | .observeOn(Schedulers.io())
102 | .doOnNext(new Consumer() {
103 | @Override
104 | public void accept(Integer integer) throws Exception {
105 | Log.d(TAG, "After observeOn(io), current thread is : " + Thread.currentThread().getName());
106 | }
107 | })
108 | .subscribe(consumer);
109 | }
110 |
111 | public static void practice1(final Context context) {
112 | Api api = RetrofitProvider.get().create(Api.class);
113 | api.login(new LoginRequest())
114 | .subscribeOn(Schedulers.io()) //在IO线程进行网络请求
115 | .observeOn(AndroidSchedulers.mainThread()) //回到主线程去处理请求结果
116 | .subscribe(new Observer() {
117 | @Override
118 | public void onSubscribe(Disposable d) {
119 | }
120 |
121 | @Override
122 | public void onNext(LoginResponse value) {
123 | }
124 |
125 | @Override
126 | public void onError(Throwable e) {
127 | Toast.makeText(context, "登录失败", Toast.LENGTH_SHORT).show();
128 | }
129 |
130 | @Override
131 | public void onComplete() {
132 | Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
133 | }
134 | });
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterThree.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 | import android.widget.Toast;
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.android.schedulers.AndroidSchedulers;
16 | import io.reactivex.functions.Consumer;
17 | import io.reactivex.functions.Function;
18 | import io.reactivex.schedulers.Schedulers;
19 | import zlc.season.rxjava2demo.Api;
20 | import zlc.season.rxjava2demo.RetrofitProvider;
21 | import zlc.season.rxjava2demo.entity.LoginRequest;
22 | import zlc.season.rxjava2demo.entity.LoginResponse;
23 | import zlc.season.rxjava2demo.entity.RegisterRequest;
24 | import zlc.season.rxjava2demo.entity.RegisterResponse;
25 |
26 | import static zlc.season.rxjava2demo.MainActivity.TAG;
27 |
28 | /**
29 | * Author: Season(ssseasonnn@gmail.com)
30 | * Date: 2016/12/9
31 | * Time: 16:55
32 | * FIXME
33 | */
34 | public class ChapterThree {
35 | public static void demo1() {
36 | Observable.create(new ObservableOnSubscribe() {
37 | @Override
38 | public void subscribe(ObservableEmitter emitter) throws Exception {
39 | emitter.onNext(1);
40 | emitter.onNext(2);
41 | emitter.onNext(3);
42 | }
43 | }).map(new Function() {
44 | @Override
45 | public String apply(Integer integer) throws Exception {
46 | return "This is result " + integer;
47 | }
48 | }).subscribe(new Consumer() {
49 | @Override
50 | public void accept(String s) throws Exception {
51 | Log.d(TAG, s);
52 | }
53 | });
54 | }
55 |
56 | public static void demo2() {
57 | Observable.create(new ObservableOnSubscribe() {
58 | @Override
59 | public void subscribe(ObservableEmitter emitter) throws Exception {
60 | emitter.onNext(1);
61 | emitter.onNext(2);
62 | emitter.onNext(3);
63 | }
64 | }).flatMap(new Function>() {
65 | @Override
66 | public ObservableSource apply(Integer integer) throws Exception {
67 | final List list = new ArrayList<>();
68 | for (int i = 0; i < 3; i++) {
69 | list.add("I am value " + integer);
70 | }
71 | return Observable.fromIterable(list).delay(10, TimeUnit.MILLISECONDS);
72 | }
73 | }).subscribe(new Consumer() {
74 | @Override
75 | public void accept(String s) throws Exception {
76 | Log.d(TAG, s);
77 | }
78 | });
79 | }
80 |
81 | public static void demo3() {
82 | Observable.create(new ObservableOnSubscribe() {
83 | @Override
84 | public void subscribe(ObservableEmitter emitter) throws Exception {
85 | emitter.onNext(1);
86 | emitter.onNext(2);
87 | emitter.onNext(3);
88 | }
89 | }).concatMap(new Function>() {
90 | @Override
91 | public ObservableSource apply(Integer integer) throws Exception {
92 | final List list = new ArrayList<>();
93 | for (int i = 0; i < 3; i++) {
94 | list.add("I am value " + integer);
95 | }
96 | return Observable.fromIterable(list).delay(10, TimeUnit.MILLISECONDS);
97 | }
98 | }).subscribe(new Consumer() {
99 | @Override
100 | public void accept(String s) throws Exception {
101 | Log.d(TAG, s);
102 | }
103 | });
104 | }
105 |
106 | public static void practice1(final Context context) {
107 | final Api api = RetrofitProvider.get().create(Api.class);
108 | api.register(new RegisterRequest()) //发起注册请求
109 | .subscribeOn(Schedulers.io()) //在IO线程进行网络请求
110 | .observeOn(AndroidSchedulers.mainThread()) //回到主线程去处理请求注册结果
111 | .doOnNext(new Consumer() {
112 | @Override
113 | public void accept(RegisterResponse registerResponse) throws Exception {
114 | //先根据注册的响应结果去做一些操作
115 | }
116 | })
117 | .observeOn(Schedulers.io()) //回到IO线程去发起登录请求
118 | .flatMap(new Function>() {
119 | @Override
120 | public ObservableSource apply(RegisterResponse registerResponse) throws Exception {
121 | return api.login(new LoginRequest());
122 | }
123 | })
124 | .observeOn(AndroidSchedulers.mainThread()) //回到主线程去处理请求登录的结果
125 | .subscribe(new Consumer() {
126 | @Override
127 | public void accept(LoginResponse loginResponse) throws Exception {
128 | Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
129 | }
130 | }, new Consumer() {
131 | @Override
132 | public void accept(Throwable throwable) throws Exception {
133 | Toast.makeText(context, "登录失败", Toast.LENGTH_SHORT).show();
134 | }
135 | });
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterSix.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import io.reactivex.Observable;
8 | import io.reactivex.ObservableEmitter;
9 | import io.reactivex.ObservableOnSubscribe;
10 | import io.reactivex.android.schedulers.AndroidSchedulers;
11 | import io.reactivex.functions.BiFunction;
12 | import io.reactivex.functions.Consumer;
13 | import io.reactivex.functions.Predicate;
14 | import io.reactivex.schedulers.Schedulers;
15 |
16 | import static zlc.season.rxjava2demo.MainActivity.TAG;
17 |
18 | /**
19 | * Author: Season(ssseasonnn@gmail.com)
20 | * Date: 2016/12/13
21 | * Time: 14:20
22 | * FIXME
23 | */
24 | public class ChapterSix {
25 | public static void demo1() {
26 | Observable.create(new ObservableOnSubscribe() {
27 | @Override
28 | public void subscribe(ObservableEmitter emitter) throws Exception {
29 | for (int i = 0; ; i++) {
30 | emitter.onNext(i);
31 | }
32 | }
33 | }).subscribeOn(Schedulers.io())
34 | .observeOn(AndroidSchedulers.mainThread())
35 | .subscribe(new Consumer() {
36 | @Override
37 | public void accept(Integer integer) throws Exception {
38 | Log.d(TAG, "" + integer);
39 | }
40 | });
41 | }
42 |
43 | public static void demo2() {
44 | Observable.create(new ObservableOnSubscribe() {
45 | @Override
46 | public void subscribe(ObservableEmitter emitter) throws Exception {
47 | for (int i = 0; ; i++) {
48 | emitter.onNext(i);
49 | }
50 | }
51 | }).subscribeOn(Schedulers.io())
52 | .filter(new Predicate() {
53 | @Override
54 | public boolean test(Integer integer) throws Exception {
55 | return integer % 10 == 0;
56 | }
57 | })
58 | .observeOn(AndroidSchedulers.mainThread())
59 | .subscribe(new Consumer() {
60 | @Override
61 | public void accept(Integer integer) throws Exception {
62 | Log.d(TAG, "" + integer);
63 | }
64 | });
65 | }
66 |
67 | public static void demo3() {
68 | Observable.create(new ObservableOnSubscribe() {
69 | @Override
70 | public void subscribe(ObservableEmitter emitter) throws Exception {
71 | for (int i = 0; ; i++) {
72 | emitter.onNext(i);
73 | }
74 | }
75 | }).subscribeOn(Schedulers.io())
76 | .sample(2, TimeUnit.SECONDS)
77 | .observeOn(AndroidSchedulers.mainThread())
78 | .subscribe(new Consumer() {
79 | @Override
80 | public void accept(Integer integer) throws Exception {
81 | Log.d(TAG, "" + integer);
82 | }
83 | });
84 | }
85 |
86 | public static void demo4() {
87 | Observable.create(new ObservableOnSubscribe() {
88 | @Override
89 | public void subscribe(ObservableEmitter emitter) throws Exception {
90 | for (int i = 0; ; i++) {
91 | emitter.onNext(i);
92 | Thread.sleep(2000);
93 | }
94 | }
95 | }).subscribeOn(Schedulers.io())
96 | .observeOn(AndroidSchedulers.mainThread())
97 | .subscribe(new Consumer() {
98 | @Override
99 | public void accept(Integer integer) throws Exception {
100 | Log.d(TAG, "" + integer);
101 | }
102 | });
103 | }
104 |
105 |
106 | public static void demo5() {
107 | Observable observable1 = Observable.create(new ObservableOnSubscribe() {
108 | @Override
109 | public void subscribe(ObservableEmitter emitter) throws Exception {
110 | for (int i = 0; ; i++) {
111 | emitter.onNext(i);
112 | }
113 | }
114 | }).subscribeOn(Schedulers.io()).sample(2, TimeUnit.SECONDS);
115 |
116 | Observable observable2 = Observable.create(new ObservableOnSubscribe() {
117 | @Override
118 | public void subscribe(ObservableEmitter emitter) throws Exception {
119 | emitter.onNext("A");
120 | }
121 | }).subscribeOn(Schedulers.io());
122 |
123 | Observable.zip(observable1, observable2, new BiFunction() {
124 | @Override
125 | public String apply(Integer integer, String s) throws Exception {
126 | return integer + s;
127 | }
128 | }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
129 | @Override
130 | public void accept(String s) throws Exception {
131 | Log.d(TAG, s);
132 | }
133 | }, new Consumer() {
134 | @Override
135 | public void accept(Throwable throwable) throws Exception {
136 | Log.w(TAG, throwable);
137 | }
138 | });
139 | }
140 |
141 | public static void demo6() {
142 | Observable observable1 = Observable.create(new ObservableOnSubscribe() {
143 | @Override
144 | public void subscribe(ObservableEmitter emitter) throws Exception {
145 | for (int i = 0; ; i++) {
146 | emitter.onNext(i);
147 | Thread.sleep(2000);
148 | }
149 | }
150 | }).subscribeOn(Schedulers.io());
151 |
152 | Observable observable2 = Observable.create(new ObservableOnSubscribe() {
153 | @Override
154 | public void subscribe(ObservableEmitter emitter) throws Exception {
155 | emitter.onNext("A");
156 | }
157 | }).subscribeOn(Schedulers.io());
158 |
159 | Observable.zip(observable1, observable2, new BiFunction() {
160 | @Override
161 | public String apply(Integer integer, String s) throws Exception {
162 | return integer + s;
163 | }
164 | }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
165 | @Override
166 | public void accept(String s) throws Exception {
167 | Log.d(TAG, s);
168 | }
169 | }, new Consumer() {
170 | @Override
171 | public void accept(Throwable throwable) throws Exception {
172 | Log.w(TAG, throwable);
173 | }
174 | });
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterSeven.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import org.reactivestreams.Subscriber;
6 | import org.reactivestreams.Subscription;
7 |
8 | import io.reactivex.BackpressureStrategy;
9 | import io.reactivex.Flowable;
10 | import io.reactivex.FlowableEmitter;
11 | import io.reactivex.FlowableOnSubscribe;
12 | import io.reactivex.android.schedulers.AndroidSchedulers;
13 | import io.reactivex.schedulers.Schedulers;
14 |
15 | import static zlc.season.rxjava2demo.MainActivity.TAG;
16 |
17 | /**
18 | * Author: Season(ssseasonnn@gmail.com)
19 | * Date: 2016/12/15
20 | * Time: 09:42
21 | * FIXME
22 | */
23 | public class ChapterSeven {
24 | public static Subscription mSubscription;
25 |
26 | public static void demo1() {
27 | Flowable upstream = Flowable.create(new FlowableOnSubscribe() {
28 | @Override
29 | public void subscribe(FlowableEmitter emitter) throws Exception {
30 | Log.d(TAG, "emit 1");
31 | emitter.onNext(1);
32 | Log.d(TAG, "emit 2");
33 | emitter.onNext(2);
34 | Log.d(TAG, "emit 3");
35 | emitter.onNext(3);
36 | Log.d(TAG, "emit complete");
37 | emitter.onComplete();
38 | }
39 | }, BackpressureStrategy.ERROR);
40 |
41 | Subscriber downstream = new Subscriber() {
42 |
43 | @Override
44 | public void onSubscribe(Subscription s) {
45 | Log.d(TAG, "onSubscribe");
46 | s.request(Long.MAX_VALUE);
47 | }
48 |
49 | @Override
50 | public void onNext(Integer integer) {
51 | Log.d(TAG, "onNext: " + integer);
52 | }
53 |
54 | @Override
55 | public void onError(Throwable t) {
56 | Log.w(TAG, "onError: ", t);
57 | }
58 |
59 | @Override
60 | public void onComplete() {
61 | Log.d(TAG, "onComplete");
62 | }
63 | };
64 |
65 | upstream.subscribe(downstream);
66 | }
67 |
68 | public static void demo2() {
69 | Flowable.create(new FlowableOnSubscribe() {
70 | @Override
71 | public void subscribe(FlowableEmitter emitter) throws Exception {
72 | Log.d(TAG, "emit 1");
73 | emitter.onNext(1);
74 | Log.d(TAG, "emit 2");
75 | emitter.onNext(2);
76 | Log.d(TAG, "emit 3");
77 | emitter.onNext(3);
78 | Log.d(TAG, "emit complete");
79 | emitter.onComplete();
80 | }
81 | }, BackpressureStrategy.ERROR).subscribe(new Subscriber() {
82 |
83 | @Override
84 | public void onSubscribe(Subscription s) {
85 | Log.d(TAG, "onSubscribe");
86 | }
87 |
88 | @Override
89 | public void onNext(Integer integer) {
90 | Log.d(TAG, "onNext: " + integer);
91 |
92 | }
93 |
94 | @Override
95 | public void onError(Throwable t) {
96 | Log.w(TAG, "onError: ", t);
97 | }
98 |
99 | @Override
100 | public void onComplete() {
101 | Log.d(TAG, "onComplete");
102 | }
103 | });
104 | }
105 |
106 | public static void request(long n) {
107 | mSubscription.request(n);
108 | }
109 |
110 | public static void demo3() {
111 | Flowable.create(new FlowableOnSubscribe() {
112 | @Override
113 | public void subscribe(FlowableEmitter emitter) throws Exception {
114 | Log.d(TAG, "emit 1");
115 | emitter.onNext(1);
116 | Log.d(TAG, "emit 2");
117 | emitter.onNext(2);
118 | Log.d(TAG, "emit 3");
119 | emitter.onNext(3);
120 | Log.d(TAG, "emit complete");
121 | emitter.onComplete();
122 | }
123 | }, BackpressureStrategy.ERROR).subscribeOn(Schedulers.io())
124 | .observeOn(AndroidSchedulers.mainThread())
125 | .subscribe(new Subscriber() {
126 |
127 | @Override
128 | public void onSubscribe(Subscription s) {
129 | Log.d(TAG, "onSubscribe");
130 | mSubscription = s;
131 | }
132 |
133 | @Override
134 | public void onNext(Integer integer) {
135 | Log.d(TAG, "onNext: " + integer);
136 | }
137 |
138 | @Override
139 | public void onError(Throwable t) {
140 | Log.w(TAG, "onError: ", t);
141 | }
142 |
143 | @Override
144 | public void onComplete() {
145 | Log.d(TAG, "onComplete");
146 | }
147 | });
148 | }
149 |
150 |
151 | public static void demo4() {
152 | Flowable.create(new FlowableOnSubscribe() {
153 | @Override
154 | public void subscribe(FlowableEmitter emitter) throws Exception {
155 | for (int i = 0; i < 128; i++) {
156 | Log.d(TAG, "emit " + i);
157 | emitter.onNext(i);
158 | }
159 | }
160 | }, BackpressureStrategy.ERROR).subscribeOn(Schedulers.io())
161 | .observeOn(AndroidSchedulers.mainThread())
162 | .subscribe(new Subscriber() {
163 |
164 | @Override
165 | public void onSubscribe(Subscription s) {
166 | Log.d(TAG, "onSubscribe");
167 | mSubscription = s;
168 | }
169 |
170 | @Override
171 | public void onNext(Integer integer) {
172 | Log.d(TAG, "onNext: " + integer);
173 | }
174 |
175 | @Override
176 | public void onError(Throwable t) {
177 | Log.w(TAG, "onError: ", t);
178 | }
179 |
180 | @Override
181 | public void onComplete() {
182 | Log.d(TAG, "onComplete");
183 | }
184 | });
185 | }
186 |
187 |
188 | public static void demo5() {
189 | Flowable upstream = Flowable.create(new FlowableOnSubscribe() {
190 | @Override
191 | public void subscribe(FlowableEmitter emitter) throws Exception {
192 | for (int i = 0; i < 129; i++) {
193 | Log.d(TAG, "emit " + i);
194 | emitter.onNext(i);
195 | }
196 | }
197 | }, BackpressureStrategy.ERROR);
198 |
199 | Subscriber downstream = new Subscriber() {
200 |
201 | @Override
202 | public void onSubscribe(Subscription s) {
203 | Log.d(TAG, "onSubscribe");
204 | mSubscription = s;
205 | s.request(Long.MAX_VALUE);
206 | }
207 |
208 | @Override
209 | public void onNext(Integer integer) {
210 | Log.d(TAG, "onNext: " + integer);
211 |
212 | }
213 |
214 | @Override
215 | public void onError(Throwable t) {
216 | Log.w(TAG, "onError: ", t);
217 | }
218 |
219 | @Override
220 | public void onComplete() {
221 | Log.d(TAG, "onComplete");
222 | }
223 | };
224 |
225 | upstream.subscribeOn(Schedulers.io())
226 | .observeOn(AndroidSchedulers.mainThread())
227 | .subscribe(downstream);
228 | }
229 | }
230 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterFour.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import io.reactivex.Observable;
6 | import io.reactivex.ObservableEmitter;
7 | import io.reactivex.ObservableOnSubscribe;
8 | import io.reactivex.Observer;
9 | import io.reactivex.android.schedulers.AndroidSchedulers;
10 | import io.reactivex.disposables.Disposable;
11 | import io.reactivex.functions.BiFunction;
12 | import io.reactivex.functions.Consumer;
13 | import io.reactivex.schedulers.Schedulers;
14 | import zlc.season.rxjava2demo.Api;
15 | import zlc.season.rxjava2demo.RetrofitProvider;
16 | import zlc.season.rxjava2demo.entity.UserBaseInfoRequest;
17 | import zlc.season.rxjava2demo.entity.UserBaseInfoResponse;
18 | import zlc.season.rxjava2demo.entity.UserExtraInfoRequest;
19 | import zlc.season.rxjava2demo.entity.UserExtraInfoResponse;
20 | import zlc.season.rxjava2demo.entity.UserInfo;
21 |
22 | import static zlc.season.rxjava2demo.MainActivity.TAG;
23 |
24 | /**
25 | * Author: Season(ssseasonnn@gmail.com)
26 | * Date: 2016/12/9
27 | * Time: 17:00
28 | * FIXME
29 | */
30 | public class ChapterFour {
31 | public static void demo1() {
32 | Observable observable1 = Observable.create(new ObservableOnSubscribe() {
33 | @Override
34 | public void subscribe(ObservableEmitter emitter) throws Exception {
35 | Log.d(TAG, "emit 1");
36 | emitter.onNext(1);
37 | Log.d(TAG, "emit 2");
38 | emitter.onNext(2);
39 | Log.d(TAG, "emit 3");
40 | emitter.onNext(3);
41 | Log.d(TAG, "emit 4");
42 | emitter.onNext(4);
43 | Log.d(TAG, "emit complete1");
44 | emitter.onComplete();
45 | }
46 | });
47 |
48 | Observable observable2 = Observable.create(new ObservableOnSubscribe() {
49 | @Override
50 | public void subscribe(ObservableEmitter emitter) throws Exception {
51 | Log.d(TAG, "emit A");
52 | emitter.onNext("A");
53 | Log.d(TAG, "emit B");
54 | emitter.onNext("B");
55 | Log.d(TAG, "emit C");
56 | emitter.onNext("C");
57 | Log.d(TAG, "emit complete2");
58 | emitter.onComplete();
59 | }
60 | });
61 |
62 | Observable.zip(observable1, observable2, new BiFunction() {
63 | @Override
64 | public String apply(Integer integer, String s) throws Exception {
65 | return integer + s;
66 | }
67 | }).subscribe(new Observer() {
68 | @Override
69 | public void onSubscribe(Disposable d) {
70 | Log.d(TAG, "onSubscribe");
71 | }
72 |
73 | @Override
74 | public void onNext(String value) {
75 | Log.d(TAG, "onNext: " + value);
76 | }
77 |
78 | @Override
79 | public void onError(Throwable e) {
80 | Log.d(TAG, "onError");
81 | }
82 |
83 | @Override
84 | public void onComplete() {
85 | Log.d(TAG, "onComplete");
86 | }
87 | });
88 | }
89 |
90 | public static void demo2() {
91 | Observable observable1 = Observable.create(new ObservableOnSubscribe() {
92 | @Override
93 | public void subscribe(ObservableEmitter emitter) throws Exception {
94 | Log.d(TAG, "emit 1");
95 | emitter.onNext(1);
96 | Thread.sleep(1000);
97 |
98 | Log.d(TAG, "emit 2");
99 | emitter.onNext(2);
100 | Thread.sleep(1000);
101 |
102 | Log.d(TAG, "emit 3");
103 | emitter.onNext(3);
104 | Thread.sleep(1000);
105 |
106 | Log.d(TAG, "emit 4");
107 | emitter.onNext(4);
108 | Thread.sleep(1000);
109 |
110 | Log.d(TAG, "emit complete1");
111 | emitter.onComplete();
112 | }
113 | });
114 |
115 | Observable observable2 = Observable.create(new ObservableOnSubscribe() {
116 | @Override
117 | public void subscribe(ObservableEmitter emitter) throws Exception {
118 | Log.d(TAG, "emit A");
119 | emitter.onNext("A");
120 | Thread.sleep(1000);
121 |
122 | Log.d(TAG, "emit B");
123 | emitter.onNext("B");
124 | Thread.sleep(1000);
125 |
126 | Log.d(TAG, "emit C");
127 | emitter.onNext("C");
128 | Thread.sleep(1000);
129 |
130 | Log.d(TAG, "emit complete2");
131 | emitter.onComplete();
132 | }
133 | });
134 |
135 | Observable.zip(observable1, observable2, new BiFunction() {
136 | @Override
137 | public String apply(Integer integer, String s) throws Exception {
138 | return integer + s;
139 | }
140 | }).subscribe(new Observer() {
141 | @Override
142 | public void onSubscribe(Disposable d) {
143 | Log.d(TAG, "onSubscribe");
144 | }
145 |
146 | @Override
147 | public void onNext(String value) {
148 | Log.d(TAG, "onNext: " + value);
149 | }
150 |
151 | @Override
152 | public void onError(Throwable e) {
153 | Log.d(TAG, "onError");
154 | }
155 |
156 | @Override
157 | public void onComplete() {
158 | Log.d(TAG, "onComplete");
159 | }
160 | });
161 | }
162 |
163 | public static void demo3() {
164 | Observable observable1 = Observable.create(new ObservableOnSubscribe() {
165 | @Override
166 | public void subscribe(ObservableEmitter emitter) throws Exception {
167 | Log.d(TAG, "emit 1");
168 | emitter.onNext(1);
169 | Thread.sleep(1000);
170 |
171 | Log.d(TAG, "emit 2");
172 | emitter.onNext(2);
173 | Thread.sleep(1000);
174 |
175 | Log.d(TAG, "emit 3");
176 | emitter.onNext(3);
177 | Thread.sleep(1000);
178 |
179 | Log.d(TAG, "emit 4");
180 | emitter.onNext(4);
181 | Thread.sleep(1000);
182 |
183 | Log.d(TAG, "emit complete1");
184 | emitter.onComplete();
185 | }
186 | }).subscribeOn(Schedulers.io());
187 |
188 | Observable observable2 = Observable.create(new ObservableOnSubscribe() {
189 | @Override
190 | public void subscribe(ObservableEmitter emitter) throws Exception {
191 | Log.d(TAG, "emit A");
192 | emitter.onNext("A");
193 | Thread.sleep(1000);
194 |
195 | Log.d(TAG, "emit B");
196 | emitter.onNext("B");
197 | Thread.sleep(1000);
198 |
199 | Log.d(TAG, "emit C");
200 | emitter.onNext("C");
201 | Thread.sleep(1000);
202 |
203 | Log.d(TAG, "emit complete2");
204 | emitter.onComplete();
205 | }
206 | }).subscribeOn(Schedulers.io());
207 |
208 | Observable.zip(observable1, observable2, new BiFunction() {
209 | @Override
210 | public String apply(Integer integer, String s) throws Exception {
211 | return integer + s;
212 | }
213 | }).subscribe(new Observer() {
214 | @Override
215 | public void onSubscribe(Disposable d) {
216 | Log.d(TAG, "onSubscribe");
217 | }
218 |
219 | @Override
220 | public void onNext(String value) {
221 | Log.d(TAG, "onNext: " + value);
222 | }
223 |
224 | @Override
225 | public void onError(Throwable e) {
226 | Log.d(TAG, "onError");
227 | }
228 |
229 | @Override
230 | public void onComplete() {
231 | Log.d(TAG, "onComplete");
232 | }
233 | });
234 | }
235 |
236 | public static void practice1(){
237 | final Api api = RetrofitProvider.get().create(Api.class);
238 | Observable observable1 =
239 | api.getUserBaseInfo(new UserBaseInfoRequest()).subscribeOn(Schedulers.io());
240 |
241 | Observable observable2 =
242 | api.getUserExtraInfo(new UserExtraInfoRequest()).subscribeOn(Schedulers.io());
243 |
244 | Observable.zip(observable1, observable2,
245 | new BiFunction() {
246 | @Override
247 | public UserInfo apply(UserBaseInfoResponse baseInfo,
248 | UserExtraInfoResponse extraInfo) throws Exception {
249 | return new UserInfo(baseInfo, extraInfo);
250 | }
251 | }).observeOn(AndroidSchedulers.mainThread())
252 | .subscribe(new Consumer() {
253 | @Override
254 | public void accept(UserInfo userInfo) throws Exception {
255 | //do something;
256 | }
257 | });
258 | }
259 | }
260 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterNine.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import org.reactivestreams.Subscriber;
6 | import org.reactivestreams.Subscription;
7 |
8 | import java.io.BufferedReader;
9 | import java.io.FileReader;
10 |
11 | import io.reactivex.BackpressureStrategy;
12 | import io.reactivex.Flowable;
13 | import io.reactivex.FlowableEmitter;
14 | import io.reactivex.FlowableOnSubscribe;
15 | import io.reactivex.android.schedulers.AndroidSchedulers;
16 | import io.reactivex.schedulers.Schedulers;
17 |
18 | import static zlc.season.rxjava2demo.MainActivity.TAG;
19 |
20 | /**
21 | * Author: Season(ssseasonnn@gmail.com)
22 | * Date: 2017/1/18
23 | * FIXME
24 | */
25 | public class ChapterNine {
26 | public static Subscription mSubscription;
27 |
28 | public static void request(long n) {
29 | mSubscription.request(n);
30 | }
31 |
32 | public static void demo1() {
33 | Flowable
34 | .create(new FlowableOnSubscribe() {
35 | @Override
36 | public void subscribe(FlowableEmitter emitter) throws Exception {
37 | Log.d(TAG, "current requested: " + emitter.requested());
38 | }
39 | }, BackpressureStrategy.ERROR)
40 | .subscribe(new Subscriber() {
41 |
42 | @Override
43 | public void onSubscribe(Subscription s) {
44 | Log.d(TAG, "onSubscribe");
45 | mSubscription = s;
46 | s.request(10);
47 | s.request(100);
48 | }
49 |
50 | @Override
51 | public void onNext(Integer integer) {
52 | Log.d(TAG, "onNext: " + integer);
53 | }
54 |
55 | @Override
56 | public void onError(Throwable t) {
57 | Log.w(TAG, "onError: ", t);
58 | }
59 |
60 | @Override
61 | public void onComplete() {
62 | Log.d(TAG, "onComplete");
63 | }
64 | });
65 | }
66 |
67 | public static void demo2() {
68 | Flowable
69 | .create(new FlowableOnSubscribe() {
70 | @Override
71 | public void subscribe(final FlowableEmitter emitter) throws Exception {
72 | Log.d(TAG, "before emit, requested = " + emitter.requested());
73 |
74 | Log.d(TAG, "emit 1");
75 | emitter.onNext(1);
76 | Log.d(TAG, "after emit 1, requested = " + emitter.requested());
77 |
78 | Log.d(TAG, "emit 2");
79 | emitter.onNext(2);
80 | Log.d(TAG, "after emit 2, requested = " + emitter.requested());
81 |
82 | Log.d(TAG, "emit 3");
83 | emitter.onNext(3);
84 | Log.d(TAG, "after emit 3, requested = " + emitter.requested());
85 |
86 | Log.d(TAG, "emit complete");
87 | emitter.onComplete();
88 |
89 | Log.d(TAG, "after emit complete, requested = " + emitter.requested());
90 | }
91 | }, BackpressureStrategy.ERROR)
92 | .subscribe(new Subscriber() {
93 |
94 | @Override
95 | public void onSubscribe(Subscription s) {
96 | Log.d(TAG, "onSubscribe");
97 | mSubscription = s;
98 | s.request(2);
99 | }
100 |
101 | @Override
102 | public void onNext(Integer integer) {
103 | Log.d(TAG, "onNext: " + integer);
104 | }
105 |
106 | @Override
107 | public void onError(Throwable t) {
108 | Log.w(TAG, "onError: ", t);
109 | }
110 |
111 | @Override
112 | public void onComplete() {
113 | Log.d(TAG, "onComplete");
114 | }
115 | });
116 | }
117 |
118 | public static void demo3() {
119 | Flowable
120 | .create(new FlowableOnSubscribe() {
121 | @Override
122 | public void subscribe(FlowableEmitter emitter) throws Exception {
123 | Log.d(TAG, "current requested: " + emitter.requested());
124 | }
125 | }, BackpressureStrategy.ERROR)
126 | .subscribeOn(Schedulers.io())
127 | .observeOn(AndroidSchedulers.mainThread())
128 | .subscribe(new Subscriber() {
129 |
130 | @Override
131 | public void onSubscribe(Subscription s) {
132 | Log.d(TAG, "onSubscribe");
133 | mSubscription = s;
134 | s.request(1000);
135 | }
136 |
137 | @Override
138 | public void onNext(Integer integer) {
139 | Log.d(TAG, "onNext: " + integer);
140 | }
141 |
142 | @Override
143 | public void onError(Throwable t) {
144 | Log.w(TAG, "onError: ", t);
145 | }
146 |
147 | @Override
148 | public void onComplete() {
149 | Log.d(TAG, "onComplete");
150 | }
151 | });
152 | }
153 |
154 | public static void demo4() {
155 | Flowable
156 | .create(new FlowableOnSubscribe() {
157 | @Override
158 | public void subscribe(FlowableEmitter emitter) throws Exception {
159 | Log.d(TAG, "First requested = " + emitter.requested());
160 | boolean flag;
161 | for (int i = 0; ; i++) {
162 | flag = false;
163 | while (emitter.requested() == 0) {
164 | if (!flag) {
165 | Log.d(TAG, "Oh no! I can't emit value!");
166 | flag = true;
167 | }
168 | }
169 | emitter.onNext(i);
170 | Log.d(TAG, "emit " + i + " , requested = " + emitter.requested());
171 | }
172 | }
173 | }, BackpressureStrategy.ERROR)
174 | .subscribeOn(Schedulers.io())
175 | .observeOn(AndroidSchedulers.mainThread())
176 | .subscribe(new Subscriber() {
177 |
178 | @Override
179 | public void onSubscribe(Subscription s) {
180 | Log.d(TAG, "onSubscribe");
181 | mSubscription = s;
182 | }
183 |
184 | @Override
185 | public void onNext(Integer integer) {
186 | Log.d(TAG, "onNext: " + integer);
187 | }
188 |
189 | @Override
190 | public void onError(Throwable t) {
191 | Log.w(TAG, "onError: ", t);
192 | }
193 |
194 | @Override
195 | public void onComplete() {
196 | Log.d(TAG, "onComplete");
197 | }
198 | });
199 | }
200 |
201 | public static void main(String[] args) {
202 | practice1();
203 | try {
204 | Thread.sleep(10000000);
205 | } catch (InterruptedException e) {
206 | e.printStackTrace();
207 | }
208 | }
209 |
210 | public static void practice1() {
211 | Flowable
212 | .create(new FlowableOnSubscribe() {
213 | @Override
214 | public void subscribe(FlowableEmitter emitter) throws Exception {
215 | try {
216 | FileReader reader = new FileReader("test.txt");
217 | BufferedReader br = new BufferedReader(reader);
218 |
219 | String str;
220 |
221 | while ((str = br.readLine()) != null && !emitter.isCancelled()) {
222 | while (emitter.requested() == 0) {
223 | if (emitter.isCancelled()) {
224 | break;
225 | }
226 | }
227 | emitter.onNext(str);
228 | }
229 |
230 | br.close();
231 | reader.close();
232 |
233 | emitter.onComplete();
234 | } catch (Exception e) {
235 | emitter.onError(e);
236 | }
237 | }
238 | }, BackpressureStrategy.ERROR)
239 | .subscribeOn(Schedulers.io())
240 | .observeOn(Schedulers.newThread())
241 | .subscribe(new Subscriber() {
242 |
243 | @Override
244 | public void onSubscribe(Subscription s) {
245 | mSubscription = s;
246 | s.request(1);
247 | }
248 |
249 | @Override
250 | public void onNext(String string) {
251 | System.out.println(string);
252 | try {
253 | Thread.sleep(2000);
254 | mSubscription.request(1);
255 | } catch (InterruptedException e) {
256 | e.printStackTrace();
257 | }
258 | }
259 |
260 | @Override
261 | public void onError(Throwable t) {
262 | }
263 |
264 | @Override
265 | public void onComplete() {
266 | }
267 | });
268 | }
269 | }
270 |
--------------------------------------------------------------------------------
/app/src/main/java/zlc/season/rxjava2demo/demo/ChapterEight.java:
--------------------------------------------------------------------------------
1 | package zlc.season.rxjava2demo.demo;
2 |
3 | import android.util.Log;
4 |
5 | import org.reactivestreams.Subscriber;
6 | import org.reactivestreams.Subscription;
7 |
8 | import java.util.concurrent.TimeUnit;
9 |
10 | import io.reactivex.BackpressureStrategy;
11 | import io.reactivex.Flowable;
12 | import io.reactivex.FlowableEmitter;
13 | import io.reactivex.FlowableOnSubscribe;
14 | import io.reactivex.android.schedulers.AndroidSchedulers;
15 | import io.reactivex.schedulers.Schedulers;
16 |
17 | import static zlc.season.rxjava2demo.MainActivity.TAG;
18 |
19 | /**
20 | * Created by Season on 2016/12/19.
21 | */
22 |
23 | public class ChapterEight {
24 | public static Subscription mSubscription;
25 |
26 | public static void request(long n) {
27 | mSubscription.request(n);
28 | }
29 |
30 | public static void demo1() {
31 | Flowable.create(new FlowableOnSubscribe() {
32 | @Override
33 | public void subscribe(FlowableEmitter emitter) throws Exception {
34 | for (int i = 0; i < 1000; i++) {
35 | Log.d(TAG, "emit " + i);
36 | emitter.onNext(i);
37 | }
38 | }
39 | }, BackpressureStrategy.BUFFER).subscribeOn(Schedulers.io())
40 | .observeOn(AndroidSchedulers.mainThread())
41 | .subscribe(new Subscriber() {
42 |
43 | @Override
44 | public void onSubscribe(Subscription s) {
45 | Log.d(TAG, "onSubscribe");
46 | mSubscription = s;
47 | }
48 |
49 | @Override
50 | public void onNext(Integer integer) {
51 | Log.d(TAG, "onNext: " + integer);
52 | }
53 |
54 | @Override
55 | public void onError(Throwable t) {
56 | Log.w(TAG, "onError: ", t);
57 | }
58 |
59 | @Override
60 | public void onComplete() {
61 | Log.d(TAG, "onComplete");
62 | }
63 | });
64 | }
65 |
66 | public static void demo2() {
67 | Flowable.create(new FlowableOnSubscribe() {
68 | @Override
69 | public void subscribe(FlowableEmitter emitter) throws Exception {
70 | for (int i = 0; ; i++) {
71 | emitter.onNext(i);
72 | }
73 | }
74 | }, BackpressureStrategy.BUFFER).subscribeOn(Schedulers.io())
75 | .observeOn(AndroidSchedulers.mainThread())
76 | .subscribe(new Subscriber() {
77 |
78 | @Override
79 | public void onSubscribe(Subscription s) {
80 | Log.d(TAG, "onSubscribe");
81 | mSubscription = s;
82 | }
83 |
84 | @Override
85 | public void onNext(Integer integer) {
86 | Log.d(TAG, "onNext: " + integer);
87 | }
88 |
89 | @Override
90 | public void onError(Throwable t) {
91 | Log.w(TAG, "onError: ", t);
92 | }
93 |
94 | @Override
95 | public void onComplete() {
96 | Log.d(TAG, "onComplete");
97 | }
98 | });
99 | }
100 |
101 | public static void demo3() {
102 | Flowable.create(new FlowableOnSubscribe() {
103 | @Override
104 | public void subscribe(FlowableEmitter emitter) throws Exception {
105 | for (int i = 0; i < 10000; i++) {
106 | emitter.onNext(i);
107 | }
108 | }
109 | }, BackpressureStrategy.DROP).subscribeOn(Schedulers.io())
110 | .observeOn(AndroidSchedulers.mainThread())
111 | .subscribe(new Subscriber() {
112 |
113 | @Override
114 | public void onSubscribe(Subscription s) {
115 | Log.d(TAG, "onSubscribe");
116 | mSubscription = s;
117 | s.request(128);
118 | }
119 |
120 | @Override
121 | public void onNext(Integer integer) {
122 | Log.d(TAG, "onNext: " + integer);
123 | }
124 |
125 | @Override
126 | public void onError(Throwable t) {
127 | Log.w(TAG, "onError: ", t);
128 | }
129 |
130 | @Override
131 | public void onComplete() {
132 | Log.d(TAG, "onComplete");
133 | }
134 | });
135 | }
136 |
137 | public static void demo4() {
138 | Flowable.create(new FlowableOnSubscribe() {
139 | @Override
140 | public void subscribe(FlowableEmitter emitter) throws Exception {
141 | for (int i = 0; i < 10000; i++) {
142 | emitter.onNext(i);
143 | }
144 | }
145 | }, BackpressureStrategy.LATEST).subscribeOn(Schedulers.io())
146 | .observeOn(AndroidSchedulers.mainThread())
147 | .subscribe(new Subscriber() {
148 |
149 | @Override
150 | public void onSubscribe(Subscription s) {
151 | Log.d(TAG, "onSubscribe");
152 | mSubscription = s;
153 | s.request(128);
154 | }
155 |
156 | @Override
157 | public void onNext(Integer integer) {
158 | Log.d(TAG, "onNext: " + integer);
159 | }
160 |
161 | @Override
162 | public void onError(Throwable t) {
163 | Log.w(TAG, "onError: ", t);
164 | }
165 |
166 | @Override
167 | public void onComplete() {
168 | Log.d(TAG, "onComplete");
169 | }
170 | });
171 | }
172 |
173 | public static void demo5() {
174 | Flowable.create(new FlowableOnSubscribe() {
175 | @Override
176 | public void subscribe(FlowableEmitter emitter) throws Exception {
177 | for (int i = 0; i < 10000; i++) {
178 | Log.d(TAG, "emit " + i);
179 | emitter.onNext(i);
180 | }
181 | }
182 | }, BackpressureStrategy.MISSING).subscribeOn(Schedulers.io()).onBackpressureBuffer()
183 | .observeOn(AndroidSchedulers.mainThread())
184 | .subscribe(new Subscriber() {
185 |
186 | @Override
187 | public void onSubscribe(Subscription s) {
188 | Log.d(TAG, "onSubscribe");
189 | mSubscription = s;
190 | }
191 |
192 | @Override
193 | public void onNext(Integer integer) {
194 | Log.d(TAG, "onNext: " + integer);
195 | }
196 |
197 | @Override
198 | public void onError(Throwable t) {
199 | Log.w(TAG, "onError: ", t);
200 | }
201 |
202 | @Override
203 | public void onComplete() {
204 | Log.d(TAG, "onComplete");
205 | }
206 | });
207 | }
208 |
209 | public static void demo6() {
210 | Flowable.interval(1, TimeUnit.MICROSECONDS)
211 | .onBackpressureDrop()
212 | .observeOn(AndroidSchedulers.mainThread())
213 | .subscribe(new Subscriber() {
214 | @Override
215 | public void onSubscribe(Subscription s) {
216 | Log.d(TAG, "onSubscribe");
217 | mSubscription = s;
218 | s.request(Long.MAX_VALUE);
219 | }
220 |
221 | @Override
222 | public void onNext(Long aLong) {
223 | Log.d(TAG, "onNext: " + aLong);
224 | try {
225 | Thread.sleep(1000);
226 | } catch (InterruptedException e) {
227 | e.printStackTrace();
228 | }
229 | }
230 |
231 | @Override
232 | public void onError(Throwable t) {
233 | Log.w(TAG, "onError: ", t);
234 | }
235 |
236 | @Override
237 | public void onComplete() {
238 | Log.d(TAG, "onComplete");
239 | }
240 | });
241 | }
242 |
243 |
244 | public static void demo7() {
245 | Flowable.create(new FlowableOnSubscribe() {
246 | @Override
247 | public void subscribe(FlowableEmitter emitter) throws Exception {
248 | for (int i = 0; ; i++) {
249 | Log.d(TAG, "emit " + i);
250 | emitter.onNext(i);
251 | }
252 | }
253 | }, BackpressureStrategy.MISSING).subscribeOn(Schedulers.io())
254 | .onBackpressureBuffer()
255 | .observeOn(AndroidSchedulers.mainThread())
256 | .subscribe(new Subscriber() {
257 |
258 | @Override
259 | public void onSubscribe(Subscription s) {
260 | Log.d(TAG, "onSubscribe");
261 | mSubscription = s;
262 | }
263 |
264 | @Override
265 | public void onNext(Integer integer) {
266 | Log.d(TAG, "onNext: " + integer);
267 | }
268 |
269 | @Override
270 | public void onError(Throwable t) {
271 | Log.w(TAG, "onError: ", t);
272 | }
273 |
274 | @Override
275 | public void onComplete() {
276 | Log.d(TAG, "onComplete");
277 | }
278 | });
279 | }
280 | }
281 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------