我正在使用雅虎天气api并使用以下链接获得结果:
现在我想用这个网址进行改装。所以请告诉我如何通过传递查询来改变这个城市。
谢谢
发布于 2016-05-12 18:40:44
结果会是这样的:
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查询。
发布于 2016-05-12 17:31:49
如果我理解得很好,你正在寻找一种方法,包括一个特定的城市到网址。下面是关于如何做到这一点的示例代码。在这个例子中,可变城市可以采用任何给定的城市名称。
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;https://stackoverflow.com/questions/37193193
复制相似问题