RxJava Observable Single类

RxJava Observable Single类 介绍

Single 类表示单值响应。单个 observable 只能发出单个成功值或错误。它不会发出 onComplete 事件。

RxJava Observable Single类 声明

public abstract class Single<T>
   extends Object
      implements SingleSource<T>

RxJava Observable Single类 协议

以下是 Single Observable 操作的顺序协议:

onSubscribe (onSuccess | onError)?

RxJava Observable Single类 示例

package com.yiidian;

import io.reactivex.Single;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;

import java.util.concurrent.TimeUnit;

public class ObservableTester  {
   public static void main(String[] args)  throws InterruptedException {
      //Create the observable
      Single<String> testSingle = Single.just("你好,一点教程网");

      //Create an observer
      Disposable disposable = testSingle
         .delay(2, TimeUnit.SECONDS, Schedulers.io())
         .subscribeWith(
         new DisposableSingleObserver<String>() {

         @Override
         public void onError(Throwable e) { 
            e.printStackTrace();
         }

         @Override
         public void onSuccess(String value) {
            System.out.println(value);
         }
      }); 
      Thread.sleep(3000);
      //start observing
      disposable.dispose();
   }
}

输出结果为:

热门文章

优秀文章