I have a problem creating a correct @GET request with NASA APOD API. According to the documentation I can add a specific date to the request using a "date" parameter, for example, date=2020-03-21: https://api.nasa.gov/planetary/apod?date=2020-03-21&api_key=DEMO_KEY In this case everything works just fine. But when I try to modify a request to load a yesterday photo I have an error. I have to pass somehow a yesterday date to the "date" parameter but I can't understand how to do it: https://api.nasa.gov/planetary/apod?date=*YESTERDAY_DATE_HERE*&api_key=DEMO_KEY
I tried to create a LocalDate variable, but I received a Build Error: An annotation argument must be a compile-time constant
I would appreciate any help!
My code:
interface NasaApiService {
val yesterdayDate: LocalDate
get() = LocalDate.now().minusDays(1)
@GET("planetary/apod?date=$yesterdayDate&api_key=" + BuildConfig.NASA_API_KEY)
fun getYesterdayPhoto(): Call<NasaPhoto>
companion object {
private const val BASE_URL = "https://api.nasa.gov/"
fun returnToVmFunction(): NasaApiService {
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(NasaApiService::class.java)
}
}
}
You can indeed only pass compile-time constants as arguments to annotations, and parameterized strings are not compile-time constants. Use Retrofit's @Query
parameters instead:
@GET("planetary/apod")
fun getYesterdayPhoto(
@Query("date") date: String,
@Query("api_key") apiKey: String,
): Call<NasaPhoto>