如何将值传递给改造 url 以获得 post 请求

How to pass a value to the retrofit url for a post request

我正在尝试提出 post 请求并进行改造以对具有 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")
            }
        })

    }

视图模型:

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,以便我可以将它传递给 url 进行改造,因为电影的 ID 是我通过捆绑获得的东西我像这样从 activity 发送到我的片段:

val idInt = arguments?.getInt("Id")

有什么想法吗?谢谢。

如果我正确理解 API 文档,您需要使用 Body 参数。看看这个例子: