I got a retrofit api interface but how to set it up properly according parameters
@GET("/users/profile/{User}?age={Age}&api_key=randomkey")
suspend fun enableVoice(
@Path("User") user: String?,
@Query("age") age: String?
): Response<UserResponse?>
I got this error and not sure how to setup api side properly
java.lang.IllegalArgumentException: URL query string "age={Age}&api_key=randomkey" must not have replace block. For dynamic query parameters use @Query.
Please let me know how to make it run
CodePudding user response:
According to this answer, I think you'll need to do something like this :
@GET("/users/profile/{User}")
suspend fun enableVoice(
@Path("User") user: String?,
@Query("age") age: String?,
@Query("api_key") key: String?,
): Response<UserResponse?>
It's also possible to use @QueryMap
, where you would supply a map of key value pairs.
@GET("/users/profile/{User}")
suspend fun enableVoice(
@Path("User") user: String?,
@QueryMap queryParams: Map<String, String>
): Response<UserResponse?>
CodePudding user response:
You do not need to explicitly add the query part in the URL. Passing it in the method with @Query annotation will automatically add it in the final URL being requested. So your request should be
@GET("/users/profile/{User}")
suspend fun enableVoice(
@Path("User") user: String?,
@Query("age") age: String?,
@Query("api_key") key: String?
): Response<UserResponse?>