java.lang.IllegalArgumentException: URL query string 对于动态查询参数使用@Query。对于方法 OpenWeatherApi.currentWeatherDetails

java.lang.IllegalArgumentException: URL query string For dynamic query parameters use @Query. for method OpenWeatherApi.currentWeatherDetails

我有如下界面

interface OpenWeatherApi {

    @GET("weather?lat={lat}&lon={lon}&appid=" + Constants.OPEN_WEATHER_API_KEY)
    suspend fun currentWeatherDetails(
        @Path("lat") lat: String,
        @Path("lon") lon: String
    ): Response<CurrentWeatherResponseSchema>

}

我正在使用以下依赖项

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'

我正在使用的Base_Url如下:

const val BASE_URL = "https://api.openweathermap.org/data/2.5/"

但是,当我测试我的代码时,出现以下代码错误

java.lang.IllegalArgumentException: URL 查询字符串“lat={lat}&lon={lon}&appid=5838e6161f0e347a9641f80bc0c8”不能有替换块。对于动态查询参数,使用@Query。 对于方法 OpenWeatherApi.currentWeatherDetails

我没有显示原始密钥,但即使使用相同的调用我也会得到相同的错误

原始的 url 或 api 调用应该类似于下面的项目:

api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key} 

我已经尝试将@Path 更改为@Query,但我仍然收到相同的错误消息

下图是邮递员的测试,似乎有效:

我只是从邮递员那里复制 url 并在我的代码中进行必要的更改,但我一直收到相同的错误代码

替换:

@GET("weather?lat={lat}&lon={lon}&appid=" + Constants.OPEN_WEATHER_API_KEY)

与:

@GET("weather?appid=" + Constants.OPEN_WEATHER_API_KEY)

然后,替换:

    @Path("lat") lat: String,
    @Path("lon") lon: String

与:

    @Query("lat") lat: String,
    @Query("lon") lon: String

可能 还需要将 appid@GET 移出 @Query,尽管我认为在@GET当它是常数时。

请参阅 the Retrofit documentation 的“URL 操作”部分。