简单的RxAndroid代码不起作用

时间:2018-05-04 19:22:36

标签: java android kotlin rx-java2 rx-android

我一直试图运行这种简单的代码和平但是它的语法错误

这是错误: The error

这是我的代码:

    package com.moein.rx_test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.internal.operators.flowable.FlowableFlatMap.subscribe
import io.reactivex.schedulers.Schedulers
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription


class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    Observable.just("Hello World")
            .subscribeOn(Schedulers.newThread())
            //each subscription is going to be on a new thread.
            .observeOn(AndroidSchedulers.mainThread())
    //observation on the main thread
    //Now our subscriber!
    .subscribe(object: Subscriber<String>(){
        override fun onComplete() {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onSubscribe(s: Subscription?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onError(e: Throwable?) {
            //TODO : Handle error here
        }

        override fun onNext(t: String?) {
            Log.e("Output",t);
        }
    })

}
}

我在导入订阅时尝试了所有这些库 libraries

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

Observable的默认消费者类型为Observer

.subscribe(object: Observer<String>(){
    override fun onComplete() {
        TODO("not implemented")
    }

    override fun onSubscribe(s: Disposable?) {
        TODO("not implemented")
    }

    override fun onError(e: Throwable?) {
        //TODO : Handle error here
    }

    override fun onNext(t: String?) {
        Log.e("Output",t);
    }
})