英文:
How to return value from Observable to Rxjava 2
问题
我遇到了一个问题,即 onNext 不能包含 return,但我需要返回这个字符串。
请求是使用 Retrofit 进行的,接口内部有一个工厂 (ApiService)。
fun getNameAnimal(name: String): String {
var nameAnimal = " "
val api = ApiService.create()
api.getAnimal("Cat")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ animal ->
// 这部分有效
Log.i(LOG, animal.name)
// 这部分无效(值为空)
nameAnimal = animal.name
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
return nameAnimal
}
在日志中,答案以我需要的格式出现。
这个方法位于一个不是 activity 或 fragment 的类中。
我该如何实现我的计划?
英文:
I ran into a problem that onNext cannot contain return, but I need to return the string.
The request is made using Retrofit with a factory inside the interface (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?
答案1
得分: 1
fun getNameAnimal(name: String): Single<String> {
val api = ApiService.create()
return api.getAnimal("Cat")
.map { animal -> animal.name }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
在活动或片段中:
apiWorkingClassInstance.getNameAnimal()
.subscribe(
{ animalName ->
Log.i(LOG, animalName)
//todo
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
感谢 Alex_Skvortsov 的建议。
英文:
fun getNameAnimal(name : String) : Single<String> {
val api = ApiService.create()
return api.getAnimal("Cat")
.map { animal -> animal.name }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
-
In activity or fragment
apiWorkingClassInstance.getNameAnimal()
.subscribe(
{ animalName ->
Log.i(LOG, animalName)
//todo
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
Thanks for the tip Alex_Skvortsov
答案2
得分: 0
以下是翻译好的部分:
一个简单且清晰的方法来实现这一点是使用接口。
- 创建你的接口
interface AnimalCallbacks {
fun onAnimalNameReturned(name: String)
}
- 让你的类实现这个接口
class MyAnimal: AnimalCallbacks {
override fun onAnimalNameReturned(name: String) {
// 在这里处理逻辑
}
}
从这里开始,你可以在方法调用中传递你的接口,并使用接口中定义的方法发送结果回来。
fun getNameAnimal(name: String, callbacks: AnimalCallbacks) {
var nameAnimal = ""
val api = ApiService.create()
api.getAnimal("Cat")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ animal ->
// 这部分工作正常
Log.i(LOG, animal.name)
// 这部分不起作用(值为空)
nameAnimal = animal.name
callbacks.onAnimalNameReturned(animal.name)
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
}
英文:
An easy and clean way to achieve this would be to use an interface.
- Create your interface
interface AnimalCallbacks {
fun onAnimalNameReturned(name: String)
}
- 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())
}
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论