如何使用 retrofit 和 kotlin Coroutines 发送 get 请求的参数?
How to send parameters for get request using retrofit and kotlin Coroutines.?
我正在学习来自
https://proandroiddev.com/suspend-what-youre-doing-retrofit-has-now-coroutines-support-c65bd09ba067。
我很难理解如何发送参数以从 MainActivity
获取请求
Webservice.kt
interface Webservice {
@GET("/todos/{id}")
suspend fun getTodo(@Path(value = "id") todoId: Int): Todo
}
TodoRepository.kt
class TodoRepository {
var client: Webservice = RetrofitClient.webservice
suspend fun getTodo(id: Int) = client.getTodo(id)
}
MainViewModel.kt
class MainViewModel : ViewModel() {
val repository: TodoRepository = TodoRepository()
val firstTodo = liveData(Dispatchers.IO) {
val retrivedTodo = repository.getTodo(1)
emit(retrivedTodo)
}
}
MainAcitvity.kt
class MainActivity : AppCompatActivity() {
lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
showFirstTodo()
}
private fun showFirstTodo() {
viewModel.getFirstTodo().observe(this, Observer {
titleTextView.text = it.title
})
}
}
GET
请求中的Path
和Query
有区别。
您可以像这样轻松传递查询字符串:
interface Webservice {
@GET("/todos/{id}")
suspend fun getTodo(@Path(value = "id") todoId: Int, @Query("name") name: String?): Todo
}
您可以像这样更改您的 viewModel 代码
private val _todo = MutableLiveData<Todo>()
val todo : LiveData<Todo> get() =_todo
//Call this method from activity
fun getTodo(arg : Int)
{
val result = //Call Coroutines here
_todo.postValue(result)
}
我正在学习来自 https://proandroiddev.com/suspend-what-youre-doing-retrofit-has-now-coroutines-support-c65bd09ba067。 我很难理解如何发送参数以从 MainActivity
获取请求Webservice.kt
interface Webservice {
@GET("/todos/{id}")
suspend fun getTodo(@Path(value = "id") todoId: Int): Todo
}
TodoRepository.kt
class TodoRepository {
var client: Webservice = RetrofitClient.webservice
suspend fun getTodo(id: Int) = client.getTodo(id)
}
MainViewModel.kt
class MainViewModel : ViewModel() {
val repository: TodoRepository = TodoRepository()
val firstTodo = liveData(Dispatchers.IO) {
val retrivedTodo = repository.getTodo(1)
emit(retrivedTodo)
}
}
MainAcitvity.kt
class MainActivity : AppCompatActivity() {
lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
showFirstTodo()
}
private fun showFirstTodo() {
viewModel.getFirstTodo().observe(this, Observer {
titleTextView.text = it.title
})
}
}
GET
请求中的Path
和Query
有区别。
您可以像这样轻松传递查询字符串:
interface Webservice {
@GET("/todos/{id}")
suspend fun getTodo(@Path(value = "id") todoId: Int, @Query("name") name: String?): Todo
}
您可以像这样更改您的 viewModel 代码
private val _todo = MutableLiveData<Todo>()
val todo : LiveData<Todo> get() =_todo
//Call this method from activity
fun getTodo(arg : Int)
{
val result = //Call Coroutines here
_todo.postValue(result)
}