2016-11-08 84 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