0
Q
使用雅虎天氣阿比
A
回答
3
這將結束是這樣的:
public interface WeatherService {
@GET("v1/public/yql")
Call<String> getWeather(@Query("q") String query);
}
然後創建如下對象:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://query.yahooapis.com")
.addConverterFactory(ScalarsConverterFactory.create())
.build();
WeatherService wService = retrofit.create(WeatherService.class);
,並運行它像這樣:
String query = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"Leeds\")&format=json";
Call<String> weather = wService.getWeather(query);
try {
Response<String> resp = weather.execute();
,則應該更換ConverterFactory以JSON和正確分析天氣輸出。
我還沒有測試過這個,只是給你一個如何使用Retrofit查詢的想法。
3
如果我沒有理解好,你正在尋找一種方式來包括某個城市的URL。以下是有關如何操作的示例代碼。在這個例子中,變量城市可以取任何給定的城市名稱。
var city = "london";
var query = "select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22"+ city +"%22)&format=json"
更新:
,那麼你可以串連查詢到基本URL是這樣的:
var baseurl = "https://query.yahooapis.com/v1/public/yql?q=" + query;