I have tried two type of value passing to an URL in Android Retrofit library, method 1 is executed without any error but method 2 throw error.
I sent parameter values by query key name with the value of the annotated parameter in Method 1 and variable substitution for the API endpoint in Method 2
Error thrown by method 2:
java.lang.IllegalArgumentException: URL query string "appid={apikey}&lat={lat}&lon={lon}&units={units}" must not have replace block. For dynamic query parameters use @Query.
My URL : data/2.5/weather?lat=77.603287&lon=12.97623&appid=f5138&units=metric
Method 1: (Executed well)
@GET("data/2.5/weather")
Call<Weather> getWeatherReport(@Query("lat") String lat,
@Query("lon") String lng,
@Query("appid") String appid,
@Query("units") String units);
Method 2: (Error)
@GET("data/2.5/weather?appid={apikey}&lat={lat}&lon={lon}&units={units}")
Call<Weather> getWeatherReport1(@Query("apikey") String apikey,
@Query("lat") String lat,
@Query("lon") String lng,
@Query("units") String units);
I have tried @Path as well as in the second method.
My Questions are 1.What is the difference between both the methods? 2.Why does the second method did not worked?
Second method won't work because
URL query string must not have replace block. For dynamic query parameters use @Query
So also using @Path annotation in that case won't work. You can dynamically assign query parameters by using @Query annotation like in first method. You can dynamically apply only Path parameters with use of @Path annotation like
@GET("data/{version}/")
Call<Weather> getWeatherReport1(@Path("version") String version);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With