Android RxJava记忆变量

Android RxJava记忆变量,android,arrays,json,string,rx-java,Android,Arrays,Json,String,Rx Java,我正在学习RxJava 我的数据: 字符串变量-命令 我的目标是: 拆分字符串(空格作为正则表达式)-完成 检查数组是否有元素-完成 将数组映射到字符串(foreach)-完成 重要事项:我必须记住拆分后的第一个字符串,因为它是订阅中必需的 我知道我的第二个字符串是可选的。如果存在,则可以是json或常规字符串 我必须知道第二个字符串(不存在/json/regular字符串)中有什么内容,并执行不同的操作(订阅) 我的代码: 我想得对吗?如何正确操作?当您flatMap您的字符串数组并跳

我正在学习RxJava

我的数据:
  • 字符串变量-命令
我的目标是:
  • 拆分字符串(空格作为正则表达式)-完成
  • 检查数组是否有元素-完成
  • 将数组映射到字符串(foreach)-完成
  • 重要事项:我必须记住拆分后的第一个字符串,因为它是订阅中必需的
  • 我知道我的第二个字符串是可选的。如果存在,则可以是json或常规字符串
  • 我必须知道第二个字符串(不存在/json/regular字符串)中有什么内容,并执行不同的操作(订阅)
我的代码:
我想得对吗?如何正确操作?

当您
flatMap
您的
字符串
数组并跳过第一个
字符串时,您将丢失第一个
字符串

如果要使用第一个和第二个字符串,则必须同时发出这两个字符串

Observable.just(command)
              .map(s -> s.split(" "))
              .filter(strings -> strings.length > 0)
              .flatMap(arr -> {
                  // transform the second args as a Json Object
                  Observable<Object> jsonObject = Observable.from(arr)
                                                            .skip(1)
                                                            .map(this::getJson);

                  // get the first String
                  Observable<String> firstArg = Observable.from(arr)
                                                          .take(1);
                  // call myMethod with the first String and the jsonObject
                  return Observable.combineLatest(firstArg, jsonObject, (first, second) -> myMethod(first, second))
                                   // call fallback method if you haven't a second arg 
                                   .switchIfEmpty(firstArg.map(f -> fallbackMethod(m)));

              })
              .subscribe();
Observable.just(命令)
.map(s->s.split(“”)
.filter(字符串->字符串.length>0)
.flatMap(arr->{
//将第二个参数转换为Json对象
可观测的jsonObject=可观测的距离(arr)
.skip(1)
.map(this::getJson);
//获取第一个字符串
可观测的第一个参数=可观测的起始值(arr)
.采取(1)项措施;
//使用第一个字符串和jsonObject调用myMethod
返回Observable.CombineTest(firstArg,jsonObject,(first,second)->myMethod(first,second))
//如果没有第二个参数,则调用回退方法
.switchIfEmpty(firstArg.map(f->fallbackMethod(m));
})
.subscribe();
Observable.just(command)
              .map(s -> s.split(" "))
              .filter(strings -> strings.length > 0)
              .flatMap(arr -> {
                  // transform the second args as a Json Object
                  Observable<Object> jsonObject = Observable.from(arr)
                                                            .skip(1)
                                                            .map(this::getJson);

                  // get the first String
                  Observable<String> firstArg = Observable.from(arr)
                                                          .take(1);
                  // call myMethod with the first String and the jsonObject
                  return Observable.combineLatest(firstArg, jsonObject, (first, second) -> myMethod(first, second))
                                   // call fallback method if you haven't a second arg 
                                   .switchIfEmpty(firstArg.map(f -> fallbackMethod(m)));

              })
              .subscribe();