[英]How to use @Query in Retrofit? Android Kotlin
我目前正在嘗試制作一個 ApiInterface。 我正在嘗試使用查詢來操作 API URL 。 到目前為止,我使用的 URL 是
https://api.themoviedb.org/3/search/movie?api_key={apiKey}&language=en-US&query={query}
上面的 url 我正在嘗試更改“查詢”所以我創建了這個接口:
@GET("3/search/movie?api_key=${BuildConfig.MOVIE_TOKEN}&language=en-US&")
fun getMovies(
@Query("query") query: String
): Call<SearchMovieResponse>
它運行良好,但如果 URL 的情況是這樣的:
https://api.themoviedb.org/3/movie/{id}?api_key=7fad718afb38c7fe3fbe9da94e0d54e6
我試圖操縱 {id} 的地方。 界面是這樣的嗎?
@GET("3/movie/{id}?api_key=${BuildConfig.MOVIE_TOKEN}")
fun getMovieById(
@Query("id") id: String
):Call<SearchDetailMovieResponse>
請幫助我了解如何利用注釋來修改 API 網址
您可以為此使用@Path 注釋。
@GET("3/movie/{id}?api_key=${BuildConfig.MOVIE_TOKEN}")
fun getMovieById(
@Path("id") id: String
):Call<SearchDetailMovieResponse>
附加信息:
如果您在 url 中間有參數並且想要給出動態值,則使用 @Path 注釋,因為您在 url 中間有 id。
例如 https://test-api/3/profile。 3 是 url 中間的參數,在這種情況下我們使用@Path
如果您在 url 中間沒有參數並且想要在 url 的末尾使用鍵值對傳遞參數,那么最好使用 @Query
例如 https://test-api/profile?user_id=3 user_id 是鍵值參數,在這種情況下我們使用@Query。
注意:據我所知,我正在分享。
在以下 URL 的情況下正確使用@Query
:
https://api.themoviedb.org/3/movie/{id}?api_key=7fad718afb38c7fe3fbe9da94e0d54e6
你需要這樣寫你的界面
@GET("3/movie/{id}")
fun getMovieById(
@Path("id") id: String,
@Query("api_key") apiKey: String = BuildConfig.MOVIE_TOKEN
):Call<SearchDetailMovieResponse>
有關詳細信息,請參閱文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.