如何将值传递给post请求的retrofit url



我正试图使改造后的请求与https://developers.themoviedb.org/3/movies/rate-movie评级电影。问题是,url本身取电影的id,像这样:

@POST("/movie/{id}/rating?api_key=${API_KEY}")
fun postRating(@Path("id") id:Int): Call<RatingResponse>

然后我还需要传递我的评级值来发出post请求。我是通过这里完成的(我想):

远程数据源:

interface RatingCallBack {
fun onSuccess()
fun onError(message: String?)
}
fun postRating(rating: Int, ratingCallback: RatingCallBack){
val service = RetrofitService.instance
.create(MovieService::class.java)
val call = service.postRating(rating)
call.enqueue(object : Callback<RatingResponse?> {
override fun onResponse(
call: Call<RatingResponse?>,
response: Response<RatingResponse?>
) {
if (response.isSuccessful) {
Log.d("d","d")
} else {
Log.d("d","d")
}
}
override fun onFailure(call: Call<RatingResponse?>, t: Throwable) {
Log.d("d","d")
}
})
}

viewmodel:

class RatingViewModel constructor(
private val remoteDataSource: MovieRemoteDataSource
): ViewModel() {

val ratingSuccess = MutableLiveData<Boolean>()
val ratingFailedMessage = MutableLiveData<String?>()
fun postRating(rating:Int){
remoteDataSource.postRating(rating, object: MovieRemoteDataSource.RatingCallBack{
override fun onSuccess(){
ratingSuccess.postValue(true)
}
override fun onError(message:String?){
ratingSuccess.postValue(false)
ratingFailedMessage.postValue(message)
}
})
}
}

在片段中(如果可以的话,稍后我会传递用户的评分):

binding.btRating.setOnClickListener {
viewModel.postRating(2)
}
问题是,我不知道我应该在哪里传递电影的id这样我就可以在retrofit中传递它到url因为电影的id是我通过一个bundle获得的东西我从我的activity发送给我的fragment像这样
val idInt = arguments?.getInt("Id")

任何想法?谢谢你。

如果我正确理解API文档,您需要使用Body参数。看看这个例子:如何传递'Body'android中Retrofit 2参数

相关内容

  • 没有找到相关文章

最新更新