public fun readBlogHtml(blog: String) : Observable<String> { return Observable.just(blog) .map{ blog -> assembleUrl(blog) } .flatMap { url -> readHtml(url) } }
搞得我有点迷乱……
而且其实flatMap是不需要的,因为Kotlin读网页源码就一句话……
public fun readHtml(url: String) : Observable<String> { return Observable.create<String> { subscriber -> subscriber.onNext(URL(url).readText()); subscriber.onCompleted() } }
还不如不用Observable……
于是就进一步变成了这样:
public fun readBlogHtml(blog: String) : Observable<String> = Observable.just(blog) .map { blog -> assembleUrl(blog) } .map { url -> readHtml(url) } private fun assembleUrl(blog: String?): String = "http://$blog.tumblr.com/page/" public fun readHtml(url: String) : String = URL(url).readText()
这代码还要不要写了?
啊?
RxJava都不用了……
可以变成这样:
fun test() { async { val html = readBlogHtml("blog") uiThread { update(html) } } } public fun readBlogHtml(blog: String) : String = readHtml(assembleUrl(blog)) private fun assembleUrl(blog: String?): String = "http://$blog.tumblr.com/page/" public fun readHtml(url: String) : String = URL(url).readText()
啊??