java - 简单的 RxAndroid 代码不起作用

标签 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

我该如何解决这个问题?

最佳答案

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);
    }
})

关于java - 简单的 RxAndroid 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181837/

相关文章:

java - 使用 struts2 将枚举映射为表单元素

java - 通过数组搜索

java - com.amazonaws.services.texttract 的 Maven 依赖项是什么?

android - 如何使 ListItem Textview 用作 Twitter 推文并获得不同的 Linkify 点击次数?

kotlin - Kotlin-when和let从列表中删除元素

java - Sonarlink 插件未安装在 Eclipse 中

android - lint 不会失败的构建

android - PrintManager.print() 并更改了 attachBaseContext 中的语言环境

kotlin - 如何在 Kotlin @Deprecated 注释中指定泛型类型参数?

android - Kotlin Gradle 依赖项中的 "implementation"是什么?