RxJava/RxAndroid: retry (long times, Predicate (? Super Throwable) predicate), rxjavarxandroid
RxJava/RxAndroid: retry (long times, Predicate Predicate)
Import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. log; import java.net. socket; import java. util. concurrent. callable; import io. reactivex. observable; import io. reactivex. android. schedulers. androidSchedulers; import io. reactivex. functions. predicate; import io. reactivex. observers. disposableObserver; import io. reactivex. schedulers. schedulers; public class MainActivity extends AppCompatActivity {private String TAG = "output"; private DisposableObserver mDisposableObserver = new DisposableObserver
() {@ Override public void onNext (Socket socket) {Log. d (TAG, "onNext:" + socket) ;}@ Override public void onComplete () {Log. d (TAG, "onComplete") ;}@ Override public void onError (Throwable e) {Log. e (TAG, e. toString (), e) ;}}; private Socket mSocket = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Observable. fromCallable (new Callable
() {@ Override public Socket call () throws Exception {Log. d (TAG, "call"); mSocket = new Socket ("127.0.0.1", 80); return mSocket ;}}). retry (3, new Predicate
() {@ Override public boolean test (Throwable throwable) throws Exception {Log. d (TAG, "test:" + throwable. toString (); if (mSocket! = Null & mSocket. isConnected () {return false;} else {return true ;}}}). subscribeOn (Schedulers. io ()). observeOn (AndroidSchedulers. mainThread ()). subscribeWith (mDisposableObserver );}}
01-22 12:34:16. 481 29902-29955/zhangphil. book D/output: call01-22 12:34:16. 483 29902-29955/zhangphil. book D/output: test: java.net. connectException: Connection refused01-22 12:34:16. 483 29902-29955/zhangphil. book D/output: call01-22 12:34:16. 485 29902-29955/zhangphil. book D/output: test: java.net. connectException: Connection refused01-22 12:34:16. 485 29902-29955/zhangphil. book D/output: call01-22 12:34:16. 486 29902-29955/zhangphil. book D/output: test: java.net. connectException: Connection refused01-22 12:34:16. 486 29902-29955/zhangphil. book D/output: call01-22 12:34:16. 592 29902-29902/zhangphil. book E/output: java.net. connectException: Connection refused java.net. connectException: Connection refused at java.net. plainSocketImpl. socketConnect (Native Method) at java.net. abstractPlainSocketImpl. doConnect (AbstractPlainSocketImpl. java: 334) at java.net. abstractPlainSocketImpl. connectToAddress (AbstractPlainSocketImpl. java: 196) at java.net. abstractPlainSocketImpl. connect (AbstractPlainSocketImpl. java: 178) at java.net. socksSocketImpl. connect (SocksSocketImpl. java: 356) at java.net. socket. connect (Socket. java: 592) at java.net. socket. connect (Socket. java: 541) at java.net. socket.
(Socket. java: 427) at java.net. Socket.
(Socket. java: 210) at zhangphil. book. mainActivity $3. call (MainActivity. java: 46) at zhangphil. book. mainActivity $3. call (MainActivity. java: 42) at io. reactivex. internal. operators. observable. observableFromCallable. subscribeActual (ObservableFromCallable. java: 42) at io. reactivex. observable. subscribe (Observable. java: 11040) at io. reactivex. internal. operators. observable. observableRetryPredicate $ RepeatObserver. subscribeNext (ObservableRetryPredicate. java: 111) at io. reactivex. internal. operators. observable. observableRetryPredicate. subscribeActual (ObservableRetryPredicate. java: 41) at io. reactivex. observable. subscribe (Observable. java: 11040) at io. reactivex. internal. operators. observable. observableSubscribeOn $ SubscribeTask. run (ObservableSubscribeOn. java: 96) at io. reactivex. schedask $ DisposeTask. run (Scheduler. java: 463) at io. reactivex. internal. schedulers. scheduledRunnable. run (ScheduledRunnable. java: 66) at io. reactivex. internal. schedulers. scheduledRunnable. call (ScheduledRunnable. java: 57) at java. util. concurrent. futureTask. run (FutureTask. java: 237) at java. util. concurrent. scheduledThreadPoolExecutor $ ScheduledFutureTask. run (ScheduledThreadPoolExecutor. java: 272) at java. util. concurrent. threadPoolExecutor. runWorker (ThreadPoolExecutor. java: 1133) at java. util. concurrent. threadPoolExecutor $ Worker. run (ThreadPoolExecutor. java: 607) at java. lang. thread. run (Thread. java: 776)