使用雅虎天气 Api

Using yahoo weather Api

我正在使用雅虎天气 api 并使用此 link 获得结果:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22london%22)&format=json

现在我想使用这个 url 进行改造。所以请告诉我如何通过查询更改城市。

谢谢

如果我理解得很好,您正在寻找一种将给定城市包含到 url 中的方法。这是有关如何执行此操作的示例代码。在示例中,变量 city 可以采用任何给定的城市名称。

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;

最终会变成这样:

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 查询。