简体   繁体   English

如何从 Observable 返回值到 Rxjava 2

[英]How to return value from Observable to Rxjava 2

I ran into a problem that onNext cannot contain return, but I need to return the string.我遇到了 onNext 不能包含 return 的问题,但我需要返回字符串。 The request is made using Retrofit with a factory inside the interface (ApiService).请求是使用 Retrofit 和接口内的工厂 (ApiService) 发出的。

    fun getNameAnimal(name : String) : String {
       var nameAnimal = " "

       val api = ApiService.create()

       api.getAnimal("Cat")
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .subscribe(
           { animal -> 
                 // It works
                 Log.i(LOG, animal.name) 
                 // It NOT works (empty value)
                 nameAnimal = animal.name
           },
           { error ->
                 Log.e(LOG, error.printStackTrace())
           }
       )
       return nameAnimal
    }

In the logs, the answer comes in the format I need.在日志中,答案以我需要的格式出现。

The method is in a class that is not an activity or fragment.该方法位于不是活动或片段的类中。 How can I implement my plan?我该如何实施我的计划?

fun getNameAnimal(name : String) : Single<String> {
   val api = ApiService.create()

   return api.getAnimal("Cat")
   .map { animal -> animal.name }
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
}

2. In activity or fragment 2. 在活动或片段中

apiWorkingClassInstance.getNameAnimal()
    .subscribe(
       { animalName -> 
             Log.i(LOG, animalName) 
             //todo
       },
       { error ->
             Log.e(LOG, error.printStackTrace())
       }
   )

Thanks for the tip Alex_Skvortsov感谢您的提示 Alex_Skvortsov

An easy and clean way to achieve this would be to use an interface.实现此目的的一种简单而干净的方法是使用接口。

  1. Create your interface创建您的界面
    interface AnimalCallbacks {
      fun onAnimalNameReturned(name: String)
    }
  1. Have your class implement the interface让你的班级实现接口
    class MyAnimal: AnimalCallbacks {

            override fun onAnimalNameReturned(name: String) {
                //handle logic here
            }
        }

From here you can pass your interface in the method call and send the result back using the method you defined in the interface.从这里您可以在方法调用中传递您的接口,并使用您在接口中定义的方法将结果发送回。

 fun getNameAnimal(name : String, callbacks: AnimalCallbacks) {
        var nameAnimal = " "

        val api = ApiService.create()

        api.getAnimal("Cat")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                { animal ->
                    // It works
                    Log.i(LOG, animal.name)
                    // It NOT works (empty value)
                    nameAnimal = animal.name
                    callbacks.onAnimalNameReturned(animal.name)
                },
                { error ->
                    Log.e(LOG, error.printStackTrace())
                }
            )
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM