2016-11-08 83 views
0

我正在使用翻新库,我想从OpenWeatherAPI获取天气预报。如何添加变量链接

我有这个。

基地网址:http://api.openweathermap.org/data/2.5/

@GET("weather?q=&units=&appid=" + API_KEY) 
Call<WeatherAPI> getWeatherCity(@Query("city") String city, @Query("units") String units); 

,但我得到不好的网址(我不知道如何修复它) -

响应{协议= HTTP/1.1,代码= 502,消息= Bad Gateway, url = http://api.openweathermap.org/data/2.5/weather?q=&units=&appid=111111111111111111111111&city=&units=metric}

回答

0

你的问题是在URL中。 如果我们打破网址,您将能够看到错误。

因此,我们有URL的主体:

http://api.openweathermap.org/data/2.5/weather

那么第一个参数

?q=但你还没有在=后添加任何东西,所以这是你的第一个问题。

则:

与=后没有再次&units=,所以这是你的下一个问题。

则:

&appid=111111111111111111111111这一点是好的。

则:

&city= =后再次一无所获。

则:

&units=metric这一点是好的。

所以你的问题是没有传递值的参数。

+0

是的,但如何将该值传递给url? – Stepan

+0

用纯文本你可以做“?q = london”,或者你可以用变量和字符串连接来做到这一点:String place =“london”; “?q =”+ place +“&city =”+ .... – MichaelStoddart

+0

这是可能的吗? '@GET(“weather?q = {city}&units = {units}&appid =”+ API_KEY) 调用 getWeatherCity(@Path(“city”)String city,@Path(“units”)String units);'它给了我一个错误 – Stepan