RxJava系列文章目录导读:
一、RxJava create操作符的用法和源码分析
二、RxJava map操作符用法详解
三、RxJava flatMap操作符用法详解
四、RxJava concatMap操作符用法详解
五、RxJava onErrorResumeNext操作符实现app与服务器间token机制
六、RxJava retryWhen操作符实现错误重试机制
七、RxJava 使用debounce操作符优化app搜索功能
八、RxJava concat操作处理多数据源
九、RxJava zip操作符在Android中的实际使用场景
十、RxJava switchIfEmpty操作符实现Android检查本地缓存逻辑判断
十一、RxJava defer操作符实现代码支持链式调用
十二、combineLatest操作符的高级使用
十三、RxJava导致Fragment Activity内存泄漏问题
十四、interval、takeWhile操作符实现获取验证码功能
1 map操作符的作用
Returns an Observable that applies a specified function to each item emitted by the source Observable and emits the results of these function applications.
下面是官方对于map操作符的流程图:
[图片上传失败...(image-8c16bc-1516513779013)]
对Observable发射的数据都应用一个函数,然后再发射最后的结果集。最后map()方法返回一个新的Observable。
2 用法示例
1 先来个简单的例子
假设我们从服务器获取了一个字符串集合,我们想里面的数据项都转成大写,然后把集合的顺序反转。如把[“this”,"is","rxJava"]转成["RXJAVA","IS",“THIS”]。
Observable.from(new String[]{"This", "is", "RxJava"})
.map(new Func1<String, String>() {
@Override
public String call(String s) {
printLog(tvLogs, "Transform Data toUpperCase: ", s);
return s.toUpperCase();
}
})
//转成List
.toList()
.map(new Func1<List<String>, List<String>>() {
@Override
public List<String> call(List<String> strings) {
printLog(tvLogs, "Transform Data Reverse List: ", strings.toString());
Collections.reverse(strings);
return strings;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Action1<List<String>>() {
@Override
public void call(List<String> s) {
printLog(tvLogs, "Consume Data ", s.toString());
}
});
输出结果:
Transform Data toUpperCase: 'This' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Transform Data toUpperCase: 'is' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Transform Data toUpperCase: 'RxJava' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Transform Data Reverse List: '[THIS, IS, RXJAVA]' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Consume Data '[RXJAVA, IS, THIS]' , Main Thread:true, Thread Name:main
来个复杂点的示例
假设我们有一个主机列表,想根据这个主机列表好获取它们的IP地址。
首先得有一个方法,根据主机获取IP:
private String getIPByUrl(String str) throws MalformedURLException, UnknownHostException {
URL urls = new URL(str);
String host = urls.getHost();
String address = InetAddress.getByName(host).toString();
int b = address.indexOf("/");
return address.substring(b + 1);
}
然后通过map操作符来实现:
private Observable<String> processUrlsIpByMap() {
return Observable.just(
"http://www.baidu.com/",
"http://www.google.com/",
"https://www.bing.com/")
.map(new Func1<String, String>() {
@Override
public String call(String s) {
try {
return s + " : " + getIPByUrl(s);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
printLog(tvLogs, "Consume Data: ", s);
}
});
}
输出结果:
Consume Data: 'http://www.baidu.com/ : 115.239.210.27'
Consume Data: 'http://www.google.com/ : 216.58.203.36'
Consume Data: 'https://www.bing.com/ : 202.89.233.104'
本文的例子放在github上https://github.com/chiclaim/android-sample/tree/master/rxjava