ip6*_*696 5 android rx-android retrofit2
我尝试使用RxAndroid
button.setOnClickListener(view -> restApiFactory.testService().getListTest(7)
.subscribeOn(Schedulers.io())
.subscribe(new Observer<List<Test>>() {
Run Code Online (Sandbox Code Playgroud)
我得到错误
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Run Code Online (Sandbox Code Playgroud)
我还没有 AndroidSchedulers.mainThread()
Pav*_*ngh 15
你需要UI从主线程操作,所以为了做到这一点你需要告诉rxandroid通知主线程上的更改所以使用
button.setOnClickListener(view -> restApiFactory.testService().getListTest(7)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
// ^^^^^^^^^^
.subscribe(new Observer<List<Test>>() {
Run Code Online (Sandbox Code Playgroud)
要获得这个,你需要有一个依赖
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
Run Code Online (Sandbox Code Playgroud)
并且您当前的依赖关系用于进行改造返回rxAndroid类型响应.
| 归档时间: |
|
| 查看次数: |
3284 次 |
| 最近记录: |