Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用雅虎天气Api_Android_Json_Retrofit2_Yahoo Weather Api - Fatal编程技术网

Android 使用雅虎天气Api

Android 使用雅虎天气Api,android,json,retrofit2,yahoo-weather-api,Android,Json,Retrofit2,Yahoo Weather Api,我正在使用yahoo weather api并通过以下链接获得结果: 现在我想使用这个url进行改造。所以,请告诉我如何通过传递查询来更改城市 谢谢你如果我理解得很好,你正在寻找一种将给定城市包含到url中的方法。下面是一个关于如何执行此操作的示例代码。在本例中,变量city可以采用任何给定的城市名称 var city = "london"; var query = "select%20*%20from%20weather.forecast%20where%20woeid%20in%20(sel

我正在使用yahoo weather api并通过以下链接获得结果:

现在我想使用这个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();
然后像这样运行它:

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,并正确解析天气输出

我没有对此进行测试,只是让您了解如何使用改装查询