0

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.

dazai
  • 766
  • 4
  • 25

1 Answers1

1

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

t3ddys
  • 135
  • 1
  • 5
  • Oh, I see.. wouldn't that be for the rating then? What about the movie's id? – dazai Aug 14 '21 at 22:18
  • @satoru Is number "2" in your code represents movie rating? – t3ddys Aug 14 '21 at 22:19
  • It does but it will be replaced later with the user's rating. And then there's the movie's id that goes in the url to post the rating to that movie – dazai Aug 14 '21 at 22:20
  • @satoru Well, then just change your method in ViewModel to `postRating(rating: Int, movieId: Int)` and call it from fragment like this: `arguments?.getInt("Id")?.let { viewModel.postRating(2, it) }` – t3ddys Aug 14 '21 at 22:25
  • okay but for that I'd need to pass the body first to the retrofit request, right? and then pass the body which would be 2 and then it which would be the path? – dazai Aug 14 '21 at 22:27
  • You would pass body and movie id together. I would do `@POST("/movie/{id}/rating?api_key=${API_KEY}") fun postRating(@Path("id") id:Int, @Body body: Map): Call`. I don't think that order of arguments matters. – t3ddys Aug 14 '21 at 22:35
  • okay but if you pass map then it gives the error Type mismatch. Required: Map Found: Int – dazai Aug 14 '21 at 22:42
  • @satoru pass mapOf("value" to movieId) instead of passing movieId directly – t3ddys Aug 14 '21 at 22:54