androidjsonretrofit2yahoo-weather-api

Using yahoo weather Api


I am using yahoo weather api and getting result using this 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

Now I want to using this url with retrofit. So please tell me how to change the city by passing query.

Thank you


Solution

  • It would end up being something like this:

    public interface WeatherService {
       @GET("v1/public/yql")
       Call<String> getWeather(@Query("q") String query);
    
    }
    

    Then create the object like this:

    Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://query.yahooapis.com")
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    
          WeatherService wService = retrofit.create(WeatherService.class);
    

    And run it like this:

    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();
    

    You should change the ConverterFactory to json and properly parse the weather output.

    I have not tested this, just giving you an idea of how to use the Retrofit query.