retrofit 2 @headers error in kotlin

感情迁移 提交于 2020-04-12 08:57:12

问题


I try to add multiple static headers with retrofit 2 (2.3.0) like this :

interface WeatherAPI {

    @Headers({
        "Accept: application/json",
        "Content-type:application/json"
    })
    @GET("/data/2.5/weather")
    fun getWeatherForCityName(@Query("q") city: String, @Query("appid") appid: String) : Call<GetWeatherResponse>;

}

I have the folowing error :

error

Any idea of my mistake?


回答1:


Braces {} isn't needed inside @Headers.

Docs: https://kotlinlang.org/docs/reference/annotations.html#arrays-as-annotation-parameters




回答2:


Use below code:

@Headers(
        "Accept: application/json",
        "Content-type:application/json"
)



回答3:


You should use:

@Headers(value = ["Accept: application/json",
          "Content-type:application/json"])

because the headers need an array parameter for it, you are using the wrong syntax in kotlin



来源:https://stackoverflow.com/questions/49027140/retrofit-2-headers-error-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!