Reputation: 716
I am trying to make a post request with retrofit to rate a movie with https://developers.themoviedb.org/3/movies/rate-movie. The thing is, the url itself takes on the movie's id like this:
@POST("/movie/{id}/rating?api_key=${API_KEY}")
fun postRating(@Path("id") id:Int): Call<RatingResponse>
And then I need to also pass my rating value to make the post request. I did that through here (I think):
Remote data source:
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")
}
})
}
The 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)
}
})
}
}
And in the fragment (I'll pass the user's rating later if this works):
binding.btRating.setOnClickListener {
viewModel.postRating(2)
}
The thing is, I don't know where I'm supposed to pass the movie's id so that I can pass it to the url in retrofit since the movie's id is something I get through a bundle I send to my fragment from my activity like this:
val idInt = arguments?.getInt("Id")
Any ideas? Thank you.
Upvotes: 0
Views: 143
Reputation: 155
If I understand API docs correctly, you need to use Body parameter. Check out this example: How to pass string in 'Body' Parameter of Retrofit 2 in android
Upvotes: 1