diff --git a/.travis.yml b/.travis.yml index 71c0b36..cc63e45 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,12 @@ language: android jdk: oraclejdk8 +before_install: +- yes | sdkmanager "platforms;android-27" env: global: - - ANDROID_API_LEVEL=25 - - ANDROID_BUILD_TOOLS_VERSION=25.0.3 + - ANDROID_API_LEVEL=27 + - ANDROID_BUILD_TOOLS_VERSION=27.0.3 android: components: diff --git a/README.md b/README.md index f980dc9..7e181bc 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,47 @@ # RxErrorHandler -[ ![Bintray](https://img.shields.io/badge/bintray-v2.0.2-brightgreen.svg) ](https://bintray.com/jessyancoding/maven/rxerrorhandler/2.0.2/link) -[ ![Build Status](https://travis-ci.org/JessYanCoding/RxErrorHandler.svg?branch=master) ](https://travis-ci.org/JessYanCoding/RxErrorHandler) -[ ![API](https://img.shields.io/badge/API-15%2B-blue.svg?style=flat-square) ](https://developer.android.com/about/versions/android-4.0.3.html) +[ ![Jcenter](https://img.shields.io/badge/Jcenter-v2.1.1-brightgreen.svg?style=flat-square) ](https://bintray.com/jessyancoding/maven/rxerrorhandler/2.1.1/link) +[ ![Build Status](https://travis-ci.org/JessYanCoding/RxErrorHandler.svg?branch=2.x) ](https://travis-ci.org/JessYanCoding/RxErrorHandler) +[ ![API](https://img.shields.io/badge/API-9%2B-blue.svg?style=flat-square) ](https://developer.android.com/about/versions/android-2.3.html) [ ![License](http://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square) ](http://www.apache.org/licenses/LICENSE-2.0) +[ ![Author](https://img.shields.io/badge/Author-JessYan-orange.svg?style=flat-square) ](https://www.jianshu.com/u/1d0c0bc634db) +[ ![QQ-Group](https://img.shields.io/badge/QQ%E7%BE%A4-455850365%20%7C%20301733278-orange.svg?style=flat-square) ](https://shang.qq.com/wpa/qunwpa?idkey=7e59e59145e6c7c68932ace10f52790636451f01d1ecadb6a652b1df234df753) ## Error Handle Of Rxjava -## Step 1 +## Download -``` -dependencies { - compile 'me.jessyan:rxerrorhandler:2.0.2' //rxjava2 -} - -dependencies { - compile 'me.jessyan:rxerrorhandler:1.0.1' //rxjava1 -} +``` gradle +implementation 'me.jessyan:rxerrorhandler:2.1.1' //rxjava2 +implementation 'me.jessyan:rxerrorhandler:1.0.1' //rxjava1 ``` -## Step 2 +## Initialization -``` +``` java RxErrorHandler rxErrorHandler = RxErrorHandler .builder() .with(this) - .responseErrorListener(new ResponseErroListener() { + .responseErrorListener(new ResponseErrorListener() { @Override                    public void handleResponseError(Context context, Throwable t) { - Log.w(TAG, "error handle"); - } + if (t instanceof UnknownHostException) { + //do something ... + } else if (t instanceof SocketTimeoutException) { + //do something ... + } else { + //handle other Exception ... + } + Log.w(TAG, "Error handle"); + } }).build(); ``` -## Step 3 +## Usage -``` +``` java Observable - .error(new Exception("erro")) + .error(new Exception("Error")) .retryWhen(new RetryWithDelay(3, 2))//retry(http connect timeout) .subscribe(new ErrorHandleSubscriber(rxErrorHandler) { @Override @@ -46,16 +50,28 @@ dependencies { } }); + + //Backpressure + Flowable + .error(new Exception("Error")) + .retryWhen(new RetryWithDelayOfFlowable(3, 2))//retry(http connect timeout) + .subscribe(new ErrorHandleSubscriberOfFlowable(rxErrorHandler) { + @Override + public void onNext(Object o) { + + } + }); ``` + ## About Me * **Email**: * **Home**: -* **掘金**: -* **简书**: +* **掘金**: +* **简书**: ## License -``` +``` Copyright 2016, jessyan Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/app/build.gradle b/app/build.gradle index 397b1e5..67f0b76 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,15 +1,15 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion rootProject.compileSdkVersion + buildToolsVersion rootProject.buildToolsVersion defaultConfig { applicationId "me.jessyan.rxerrorhandler.demo" - minSdkVersion 15 - targetSdkVersion 25 - versionCode 3 - versionName "2.0.2" + minSdkVersion 14 + targetSdkVersion rootProject.targetSdkVersion + versionCode rootProject.versionCode + versionName rootProject.versionName } buildTypes { release { @@ -24,9 +24,8 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:25.3.1' -// compile 'me.jessyan:rxerrorhandler:2.0.2' - compile project(':rxerrorhandler') + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'io.reactivex.rxjava2:rxjava:2.1.12' +// implementation 'me.jessyan:rxerrorhandler:2.1.1' + implementation project(':rxerrorhandler') } diff --git a/app/src/androidTest/java/me/jessyan/rxerrorhandler/demo/ApplicationTest.java b/app/src/androidTest/java/me/jessyan/rxerrorhandler/demo/ApplicationTest.java deleted file mode 100644 index 0c5f748..0000000 --- a/app/src/androidTest/java/me/jessyan/rxerrorhandler/demo/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package me.jessyan.rxerrorhandler.demo; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ee9cb62..44247ab 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="me.jessyan.rxerrorhandler.demo"> Contact me + * Follow me + * ================================================ + */ +public class App extends Application { + private final String TAG = getClass().getSimpleName(); + private RxErrorHandler mRxErrorHandler; + + @Override + public void onCreate() { + super.onCreate(); + //Initialization + mRxErrorHandler = RxErrorHandler + .builder() + .with(this) + .responseErrorListener(new ResponseErrorListener() { + @Override + public void handleResponseError(Context context, Throwable t) { + if (t instanceof UnknownHostException) { + //do something ... + } else if (t instanceof SocketTimeoutException) { + //do something ... + } else if (t instanceof ParseException || t instanceof JSONException) { + //do something ... + } else { + //handle other Exception ... + } + Log.w(TAG, "Error handle"); + } + }).build(); + } + + public RxErrorHandler getRxErrorHandler() { + return mRxErrorHandler; + } +} diff --git a/app/src/main/java/me/jessyan/rxerrorhandler/demo/MainActivity.java b/app/src/main/java/me/jessyan/rxerrorhandler/demo/MainActivity.java index 8aa1df1..46b704f 100644 --- a/app/src/main/java/me/jessyan/rxerrorhandler/demo/MainActivity.java +++ b/app/src/main/java/me/jessyan/rxerrorhandler/demo/MainActivity.java @@ -1,16 +1,38 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.jessyan.rxerrorhandler.demo; -import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.util.Log; +import io.reactivex.Flowable; import io.reactivex.Observable; import me.jessyan.rxerrorhandler.core.RxErrorHandler; import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber; +import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriberOfFlowable; import me.jessyan.rxerrorhandler.handler.RetryWithDelay; -import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener; +import me.jessyan.rxerrorhandler.handler.RetryWithDelayOfFlowable; +/** + * ================================================ + * Created by JessYan on 9/2/2016 13:27 + * Contact me + * Follow me + * ================================================ + */ public class MainActivity extends AppCompatActivity { private final String TAG = getClass().getSimpleName(); @@ -19,19 +41,9 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - - RxErrorHandler rxErrorHandler = RxErrorHandler - .builder() - .with(this) - .responseErrorListener(new ResponseErrorListener() { - @Override - public void handleResponseError(Context context, Throwable t) { - Log.w(TAG, "error handle"); - } - }).build(); - + RxErrorHandler rxErrorHandler = ((App) getApplicationContext()).getRxErrorHandler(); Observable - .error(new Exception("erro")) + .error(new Exception("Error")) .retryWhen(new RetryWithDelay(3, 2))//retry(http connect timeout) .subscribe(new ErrorHandleSubscriber(rxErrorHandler) { @Override @@ -39,5 +51,16 @@ public void onNext(Object o) { } }); + + Flowable //Backpressure + .error(new Exception("Error")) + .retryWhen(new RetryWithDelayOfFlowable(3, 2))//retry(http connect timeout) + .subscribe(new ErrorHandleSubscriberOfFlowable(rxErrorHandler) { + @Override + public void onNext(Object o) { + + } + }); + } } diff --git a/app/src/test/java/me/jessyan/rxerrorhandler/demo/ExampleUnitTest.java b/app/src/test/java/me/jessyan/rxerrorhandler/demo/ExampleUnitTest.java deleted file mode 100644 index 3e271c4..0000000 --- a/app/src/test/java/me/jessyan/rxerrorhandler/demo/ExampleUnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.jessyan.rxerrorhandler.demo; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/build.gradle b/build.gradle index c4b1745..dfc94cc 100644 --- a/build.gradle +++ b/build.gradle @@ -3,10 +3,11 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.2' - classpath 'com.novoda:bintray-release:0.4.0' + classpath 'com.android.tools.build:gradle:3.1.3' + classpath 'com.novoda:bintray-release:0.8.0' } } @@ -14,6 +15,8 @@ buildscript { allprojects { repositories { jcenter() + google() + maven { url "https://maven.google.com" } } } @@ -22,3 +25,12 @@ allprojects { task clean(type: Delete) { delete rootProject.buildDir } + +ext { + minSdkVersion = 9 + targetSdkVersion = 27 + compileSdkVersion = 27 + buildToolsVersion = "27.0.3" + versionCode = 11 + versionName = "2.1.1" +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ad9a23e..0119a97 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 03 22:49:52 CST 2017 +#Sat Jul 07 15:31:53 CST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/rxerrorhandler/bintray.gradle b/rxerrorhandler/bintray.gradle index 0908099..667c017 100644 --- a/rxerrorhandler/bintray.gradle +++ b/rxerrorhandler/bintray.gradle @@ -19,7 +19,7 @@ publish { userOrg = 'jessyancoding' //bintray注册的用户名 groupId = 'me.jessyan' //compile引用时的第1部分groupId artifactId = 'rxerrorhandler' //compile引用时的第2部分项目名 - publishVersion = '2.0.2' //compile引用时的第3部分版本号 + publishVersion = rootProject.versionName //compile引用时的第3部分版本号 desc = 'Error Handle Of Rxjava' website = siteUrl } diff --git a/rxerrorhandler/build.gradle b/rxerrorhandler/build.gradle index e4b7fba..e3f2aff 100644 --- a/rxerrorhandler/build.gradle +++ b/rxerrorhandler/build.gradle @@ -1,14 +1,15 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion rootProject.compileSdkVersion + buildToolsVersion rootProject.buildToolsVersion defaultConfig { - minSdkVersion 15 - targetSdkVersion 25 - versionCode 3 - versionName "2.0.2" + minSdkVersion rootProject.minSdkVersion + targetSdkVersion rootProject.targetSdkVersion + versionCode rootProject.versionCode + versionName rootProject.versionName + consumerProguardFiles 'proguard-rules.pro' } buildTypes { release { @@ -23,12 +24,7 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:25.3.1' - compile 'io.reactivex.rxjava2:rxjava:2.1.0' - + compileOnly 'io.reactivex.rxjava2:rxjava:2.1.12' } - apply from: 'bintray.gradle' \ No newline at end of file diff --git a/rxerrorhandler/proguard-rules.pro b/rxerrorhandler/proguard-rules.pro index 51ad771..7d5cd80 100644 --- a/rxerrorhandler/proguard-rules.pro +++ b/rxerrorhandler/proguard-rules.pro @@ -15,3 +15,6 @@ #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} + +-keep class me.jessyan.rxerrorhandler.** { *; } +-keep interface me.jessyan.rxerrorhandler.** { *; } \ No newline at end of file diff --git a/rxerrorhandler/src/androidTest/java/me/jessyan/rxerrorhandler/ApplicationTest.java b/rxerrorhandler/src/androidTest/java/me/jessyan/rxerrorhandler/ApplicationTest.java deleted file mode 100644 index fe1feb3..0000000 --- a/rxerrorhandler/src/androidTest/java/me/jessyan/rxerrorhandler/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package me.jessyan.rxerrorhandler; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/core/RxErrorHandler.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/core/RxErrorHandler.java index b3aef01..af6c722 100644 --- a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/core/RxErrorHandler.java +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/core/RxErrorHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.jessyan.rxerrorhandler.core; import android.content.Context; @@ -6,8 +21,11 @@ import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener; /** - * Created by jess on 9/2/16 13:27 - * Contact with jess.yan.effort@gmail.com + * ================================================ + * Created by JessYan on 9/2/2016 13:27 + * Contact me + * Follow me + * ================================================ */ public class RxErrorHandler { public final String TAG = this.getClass().getSimpleName(); @@ -34,11 +52,15 @@ private Builder() { } public Builder with(Context context) { + if (context == null) + throw new NullPointerException("Context cannot be null"); this.context = context; return this; } public Builder responseErrorListener(ResponseErrorListener responseErrorListener) { + if (responseErrorListener == null) + throw new NullPointerException("responseErrorListener cannot be null"); this.mResponseErrorListener = responseErrorListener; return this; } diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriber.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriber.java index 2e3ec7c..b96d114 100644 --- a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriber.java +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriber.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.jessyan.rxerrorhandler.handler; import io.reactivex.Observer; @@ -6,14 +21,16 @@ import me.jessyan.rxerrorhandler.core.RxErrorHandler; /** - * Created by jess on 9/2/16 14:41 - * Contact with jess.yan.effort@gmail.com + * ================================================ + * Created by JessYan on 9/2/2016 14:41 + * Contact me + * Follow me + * ================================================ */ - public abstract class ErrorHandleSubscriber implements Observer { private ErrorHandlerFactory mHandlerFactory; - public ErrorHandleSubscriber(RxErrorHandler rxErrorHandler){ + public ErrorHandleSubscriber(RxErrorHandler rxErrorHandler) { this.mHandlerFactory = rxErrorHandler.getHandlerFactory(); } @@ -31,9 +48,11 @@ public void onComplete() { @Override - public void onError(@NonNull Throwable e) { - e.printStackTrace(); - mHandlerFactory.handleError(e); + public void onError(@NonNull Throwable t) { + t.printStackTrace(); + //如果你某个地方不想使用全局错误处理,则重写 onError(Throwable) 并将 super.onError(e); 删掉 + //如果你不仅想使用全局错误处理,还想加入自己的逻辑,则重写 onError(Throwable) 并在 super.onError(e); 后面加入自己的逻辑 + mHandlerFactory.handleError(t); } } diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriberOfFlowable.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriberOfFlowable.java new file mode 100644 index 0000000..1407f96 --- /dev/null +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandleSubscriberOfFlowable.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.jessyan.rxerrorhandler.handler; + +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; + +import io.reactivex.annotations.NonNull; +import me.jessyan.rxerrorhandler.core.RxErrorHandler; + +/** + * ================================================ + * Created by JessYan on 9/22/2017 15:10 + * Contact me + * Follow me + * ================================================ + */ +public abstract class ErrorHandleSubscriberOfFlowable implements Subscriber { + private ErrorHandlerFactory mHandlerFactory; + + public ErrorHandleSubscriberOfFlowable(RxErrorHandler rxErrorHandler) { + this.mHandlerFactory = rxErrorHandler.getHandlerFactory(); + } + + @Override + public void onSubscribe(Subscription s) { + + } + + @Override + public void onComplete() { + + } + + + @Override + public void onError(@NonNull Throwable t) { + t.printStackTrace(); + //如果你某个地方不想使用全局错误处理,则重写 onError(Throwable) 并将 super.onError(e); 删掉 + //如果你不仅想使用全局错误处理,还想加入自己的逻辑,则重写 onError(Throwable) 并在 super.onError(e); 后面加入自己的逻辑 + mHandlerFactory.handleError(t); + } +} + diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandlerFactory.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandlerFactory.java index d6a35e0..5f74575 100644 --- a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandlerFactory.java +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/ErrorHandlerFactory.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.jessyan.rxerrorhandler.handler; import android.content.Context; @@ -5,8 +20,11 @@ import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener; /** - * Created by jess on 9/2/16 13:47 - * Contact with jess.yan.effort@gmail.com + * ================================================ + * Created by JessYan on 9/2/2016 13:47 + * Contact me + * Follow me + * ================================================ */ public class ErrorHandlerFactory { public final String TAG = this.getClass().getSimpleName(); @@ -19,7 +37,8 @@ public ErrorHandlerFactory(Context mContext, ResponseErrorListener mResponseErro } /** - * 处理错误 + * 处理错误 + * * @param throwable */ public void handleError(Throwable throwable) { diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelay.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelay.java index 55b81d4..f16826e 100644 --- a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelay.java +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelay.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.jessyan.rxerrorhandler.handler; import android.util.Log; @@ -10,8 +25,11 @@ import io.reactivex.functions.Function; /** - * Created by jess on 9/2/16 14:32 - * Contact with jess.yan.effort@gmail.com + * ================================================ + * Created by JessYan on 9/2/16 14:32 + * Contact me + * Follow me + * ================================================ */ public class RetryWithDelay implements Function, ObservableSource> { @@ -28,14 +46,13 @@ public RetryWithDelay(int maxRetries, int retryDelaySecond) { @Override public ObservableSource apply(@NonNull Observable throwableObservable) throws Exception { - return throwableObservable .flatMap(new Function>() { @Override public ObservableSource apply(@NonNull Throwable throwable) throws Exception { if (++retryCount <= maxRetries) { // When this Observable calls onNext, the original Observable will be retried (i.e. re-subscribed). - Log.d(TAG, "get error, it will try after " + retryDelaySecond + Log.d(TAG, "Observable get error, it will try after " + retryDelaySecond + " second, retry count " + retryCount); return Observable.timer(retryDelaySecond, TimeUnit.SECONDS); diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelayOfFlowable.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelayOfFlowable.java new file mode 100644 index 0000000..1e89f0b --- /dev/null +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/RetryWithDelayOfFlowable.java @@ -0,0 +1,66 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.jessyan.rxerrorhandler.handler; + +import android.util.Log; + +import org.reactivestreams.Publisher; + +import java.util.concurrent.TimeUnit; + +import io.reactivex.Flowable; +import io.reactivex.annotations.NonNull; +import io.reactivex.functions.Function; + +/** + * ================================================ + * Created by JessYan on 9/22/2017 15:25 + * Contact me + * Follow me + * ================================================ + */ +public class RetryWithDelayOfFlowable implements + Function, Publisher> { + + public final String TAG = this.getClass().getSimpleName(); + private final int maxRetries; + private final int retryDelaySecond; + private int retryCount; + + public RetryWithDelayOfFlowable(int maxRetries, int retryDelaySecond) { + this.maxRetries = maxRetries; + this.retryDelaySecond = retryDelaySecond; + } + + @Override + public Publisher apply(@NonNull Flowable throwableFlowable) throws Exception { + return throwableFlowable + .flatMap(new Function>() { + @Override + public Publisher apply(@NonNull Throwable throwable) throws Exception { + if (++retryCount <= maxRetries) { + // When this Observable calls onNext, the original Observable will be retried (i.e. re-subscribed). + Log.d(TAG, "Flowable get error, it will try after " + retryDelaySecond + + " second, retry count " + retryCount); + return Flowable.timer(retryDelaySecond, + TimeUnit.SECONDS); + } + // Max retries hit. Just pass the error along. + return Flowable.error(throwable); + } + }); + } +} \ No newline at end of file diff --git a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/listener/ResponseErrorListener.java b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/listener/ResponseErrorListener.java index 60fad20..c3661ce 100644 --- a/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/listener/ResponseErrorListener.java +++ b/rxerrorhandler/src/main/java/me/jessyan/rxerrorhandler/handler/listener/ResponseErrorListener.java @@ -1,10 +1,28 @@ +/* + * Copyright 2017 JessYan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.jessyan.rxerrorhandler.handler.listener; import android.content.Context; /** - * Created by jess on 9/2/16 13:58 - * Contact with jess.yan.effort@gmail.com + * ================================================ + * Created by JessYan on 9/2/2016 13:58 + * Contact me + * Follow me + * ================================================ */ public interface ResponseErrorListener { void handleResponseError(Context context, Throwable t); diff --git a/rxerrorhandler/src/test/java/me/jessyan/rxerrorhandler/ExampleUnitTest.java b/rxerrorhandler/src/test/java/me/jessyan/rxerrorhandler/ExampleUnitTest.java deleted file mode 100644 index 51c5772..0000000 --- a/rxerrorhandler/src/test/java/me/jessyan/rxerrorhandler/ExampleUnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.jessyan.rxerrorhandler; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file