1
votes

I have the following interface

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>

}

I am using the following dependencies

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

The Base_Url that I am using is the following :

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

However, when I testing my code I get the following code error

java.lang.IllegalArgumentException: URL query string "lat={lat}&lon={lon}&appid=5838e6161f0e347a9641f80bc0c8" must not have replace block. For dynamic query parameters use @Query. for method OpenWeatherApi.currentWeatherDetails

I am not displaying the orginal key, but even with the same call I get the same error

The original url or api call is supposed to look like the item below :

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

I have tried changing @Path to @Query and I still get the same error message

The image below is a test from postman, which seems to work:

Postman call

I just copy the url from postman and make the necessary changes in my code and I keep getting the same error code

1

1 Answers

2
votes

Replace:

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

with:

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

Then, replace:

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

with:

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

You might also need to move appid out of the @GET and into a @Query, though I think it's OK in the @GET when it is a constant.

See the "URL Manipulation" section of the Retrofit documentation.