function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nirmal Christopher 11Nirmal Christopher 11 

Easy Integration task need urgent help

Requirement :

In your Salesforce dev org, create a VF page where you can enter the name of a city. And on hitting "Fetch" button, it should fetch and display the current weather and 7 day forecast for that place.

Use Yahoo weather service API for getting the details.
https://developer.yahoo.com/weather/

this is the class I created

public class fetchweather{ public static HttpResponse makeGetCallout() { Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('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%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'); request.setMethod('GET'); HttpResponse response = http.send(request); if (response.getStatusCode() == 200) { // Deserializes the JSON string into collections of primitive data types. Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody()); system.debug(results ); } return null;

}

I called this fetch weather method in the visual force page with a controllers method. I am getting error

System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = 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%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
Error is in expression '{!fetchdata}' in component <apex:commandButton> in page searchaccounts: Class.fetchweather.makeGetCallout: line 7, column 1
Class.searchAccounts.fetchdata: line 8, column 1

I know well that i am messed up with the end point URL.
Also I am not sure what to add in the remote site settings in the end point URL.

Can some one guide me how to do this??
Nirmal Christopher 11Nirmal Christopher 11
This is the updated code...
public class fetchweather{
    public static string makeGetCallout(string city) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        string endpoint  = 'https://query.yahooapis.com/v1/public/yql?q=';
        string endpointurl = endpoint  +'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="nome, ak")';
        System.debug(endpointurl+'++++');
        request.setEndpoint(endpointurl);
        request.setMethod('GET');
        // The response is in JSON format.
        HttpResponse response = http.send(request);
        System.debug(response.getBody()+'^^^^^^');
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
             Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
             system.debug(results );
            }
    return null;
    }
}
Please give me the updated pseudocode it will be helpful.
 
Mani RenusMani Renus
You need to add your end point URL(https://query.yahooapis.com) in Remote Site Settings!
Follow Steps:
 
  1. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  2. Click New Remote Site.
  3. Enter the URL for the remote site.
  4. Click Save.

Example : 
String endpointurl = endpoint +'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="Hyderabad, IN")';
 

Hope it helps you!!

Nirmal Christopher 11Nirmal Christopher 11
The remote site settings are already configured. Below is my updated code thought the isue might be with URL encoding but I am geeting this error after updating the code.
public class fetchweather{
    public static string makeGetCallout(string city) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        string endpoint  = 'https://query.yahooapis.com/v1/public/yql?q=';
        String encodedURL = EncodingUtil.urlEncode('select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="nome, ak")', 'UTF-8');
        string endpointurl = endpoint  + encodedURL ;
        System.debug(endpointurl+'++++');
        request.setEndpoint(encodedURL);
        request.setMethod('GET');
        // The response is in JSON format.
        HttpResponse response = http.send(request);
        System.debug(response.getBody()+'^^^^^^');
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
             Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
             system.debug(results );
            }
    return null;
    }
}

Im getting this error in the visual force page where i am calling the web service call out method
System.CalloutException: no protocol: select+*+from+weather.forecast+where+woeid+in+%28select+woeid+from+geo.places%281%29+where+text%3D%22nome%2C+ak%22%29
Error is in expression '{!fetchdata}' in component <apex:commandButton> in page searchaccounts: Class.fetchweather.makeGetCallout: line 14, column 1
Class.searchAccounts.fetchdata: line 10, column 1
 
Mani RenusMani Renus

You done almost!!

Use this 
request.setEndpoint(endpointurl ); 

Instead of yours request.setEndpoint(encodedURL);



 

Nirmal Christopher 11Nirmal Christopher 11
Hi mani thanks for the reply I got the code working i get the below JSON 
{"query":{"count":1,"created":"2016-04-26T05:43:52Z","lang":"en-US","results":{"channel":{"units":{"distance":"mi","pressure":"in","speed":"mph","temperature":"F"},"title":"Yahoo! Weather - Nome, AK, US","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/","description":"Yahoo! Weather for Nome, AK, US","language":"en-us","lastBuildDate":"Mon, 25 Apr 2016 09:43 PM AKDT","ttl":"60","location":{"city":"Nome","country":"United States","region":" AK"},"wind":{"chill":"34","direction":"23","speed":"25"},"atmosphere":{"humidity":"61","pressure":"1000.0","rising":"0","visibility":"16.1"},"astronomy":{"sunrise":"6:49 am","sunset":"11:12 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Nome, AK, US at 08:00 PM AKDT","lat":"64.499474","long":"-165.405792","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/","pubDate":"Mon, 25 Apr 2016 08:00 PM AKDT","condition":{"code":"23","date":"Mon, 25 Apr 2016 08:00 PM AKDT","temp":"41","text":"Breezy"},"forecast":[{"code":"30","date":"25 Apr 2016","day":"Mon","high":"44","low":"29","text":"Partly Cloudy"},{"code":"28","date":"26 Apr 2016","day":"Tue","high":"47","low":"33","text":"Mostly Cloudy"},{"code":"28","date":"27 Apr 2016","day":"Wed","high":"45","low":"34","text":"Mostly Cloudy"},{"code":"28","date":"28 Apr 2016","day":"Thu","high":"44","low":"35","text":"Mostly Cloudy"},{"code":"30","date":"29 Apr 2016","day":"Fri","high":"43","low":"34","text":"Partly Cloudy"},{"code":"30","date":"30 Apr 2016","day":"Sat","high":"44","low":"32","text":"Partly Cloudy"},{"code":"34","date":"01 May 2016","day":"Sun","high":"42","low":"28","text":"Mostly Sunny"},{"code":"28","date":"02 May 2016","day":"Mon","high":"39","low":"31","text":"Mostly Cloudy"},{"code":"28","date":"03 May 2016","day":"Tue","high":"42","low":"34","text":"Mostly Cloudy"},{"code":"28","date":"04 May 2016","day":"Wed","high":"44","low":"35","text":"Mostly Cloudy"}],"description":"<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/23.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Breezy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Mon - Partly Cloudy. High: 44Low: 29\n<BR /> Tue - Mostly Cloudy. High: 47Low: 33\n<BR /> Wed - Mostly Cloudy. High: 45Low: 34\n<BR /> Thu - Mostly Cloudy. High: 44Low: 35\n<BR /> Fri - Partly Cloudy. High: 43Low: 34\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>","guid":{"isPermaLink":"false"}}}}}}

and this is the code  
public class fetchweather{
    public static string makeGetCallout(string city) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        string endpoint  = 'https://query.yahooapis.com/v1/public/yql?q=';
        String encodedURL = EncodingUtil.urlEncode('select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="nome, ak")', 'UTF-8');
        string endpointurl = endpoint  + encodedURL + '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys' ;
        System.debug(endpointurl+'++++');
        request.setEndpoint(endpointurl );
        request.setMethod('GET');
        // The response is in JSON format.
        HttpResponse response = http.send(request);
         Map<String, Object> res = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
          Map<String, Object> query= new Map<String, Object>();
         system.debug(res + 'results ');
     
        system.debug((String)query.get('cls_item.condition.code')+'RRRRRRRR');
     return string.valueof(res);
    }
}

I need help in parsing this kindly help
Mani RenusMani Renus

Hi,

I just did in my org with follow code. Try out in your org aswell.  Hope it helps you !!

public class YahooWeatherJsonOnVF{

public YahooWeatherJsonOnVF(){


String yahooResponse='{"query":{"count":1,"created":"2016-04-26T05:43:52Z","lang":"en-US","results":{"channel":{"units":{"distance":"mi","pressure":"in","speed":"mph","temperature":"F"},"title":"Yahoo! Weather - Nome, AK, US","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/","description":"Yahoo! Weather for Nome, AK, US","language":"en-us","lastBuildDate":"Mon, 25 Apr 2016 09:43 PM AKDT","ttl":"60","location":{"city":"Nome","country":"United States","region":" AK"},"wind":{"chill":"34","direction":"23","speed":"25"},"atmosphere":{"humidity":"61","pressure":"1000.0","rising":"0","visibility":"16.1"},"astronomy":{"sunrise":"6:49 am","sunset":"11:12 pm"},"image":{"title":"Yahoo! Weather","width":"142","height":"18","link":"http://weather.yahoo.com","url":"http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"},"item":{"title":"Conditions for Nome, AK, US at 08:00 PM AKDT","lat":"64.499474","long":"-165.405792","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/","pubDate":"Mon, 25 Apr 2016 08:00 PM AKDT","condition":{"code":"23","date":"Mon, 25 Apr 2016 08:00 PM AKDT","temp":"41","text":"Breezy"},"forecast":[{"code":"30","date":"25 Apr 2016","day":"Mon","high":"44","low":"29","text":"Partly Cloudy"},{"code":"28","date":"26 Apr 2016","day":"Tue","high":"47","low":"33","text":"Mostly Cloudy"},{"code":"28","date":"27 Apr 2016","day":"Wed","high":"45","low":"34","text":"Mostly Cloudy"},{"code":"28","date":"28 Apr 2016","day":"Thu","high":"44","low":"35","text":"Mostly Cloudy"},{"code":"30","date":"29 Apr 2016","day":"Fri","high":"43","low":"34","text":"Partly Cloudy"},{"code":"30","date":"30 Apr 2016","day":"Sat","high":"44","low":"32","text":"Partly Cloudy"},{"code":"34","date":"01 May 2016","day":"Sun","high":"42","low":"28","text":"Mostly Sunny"},{"code":"28","date":"02 May 2016","day":"Mon","high":"39","low":"31","text":"Mostly Cloudy"},{"code":"28","date":"03 May 2016","day":"Tue","high":"42","low":"34","text":"Mostly Cloudy"},{"code":"28","date":"04 May 2016","day":"Wed","high":"44","low":"35","text":"Mostly Cloudy"}],"description":"No Description as of now","guid":{"isPermaLink":"false"}}}}}}';

jObj=parse(yahooResponse);

}

public YahooWrapper jObj{get;set;} // use jObj to display values on VF

    public static YahooWrapper parse(String json) {
        return (YahooWrapper)System.JSON.deserialize(json, YahooWrapper.class);
    }

public class Condition {
        public String code {get;set;} 
        public String date2 {get;set;} 
        public String temp {get;set;} 
        public String text {get;set;} 

    }
    
    public class Wind {
        public String chill {get;set;} 
        public String direction {get;set;} 
        public String speed {get;set;} 

    }
    
    public class YahooWrapper {
        public Query query {get;set;} 

    }
    
    public class Query {
        public Integer count {get;set;} 
        public String created {get;set;} 
        public String lang {get;set;} 
        public Results results {get;set;} 

    }
    
    public class Channel {
        public Units units {get;set;} 
        public String title {get;set;} 
        public String link {get;set;} 
        public String description {get;set;} 
        public String language {get;set;} 
        public String lastBuildDate {get;set;} 
        public String ttl {get;set;} 
        public Location location {get;set;} 
        public Wind wind {get;set;} 
        public Atmosphere atmosphere {get;set;} 
        public Astronomy astronomy {get;set;} 
        public Image image {get;set;} 
        public Item item {get;set;} 

    }
    
    public class Guid {
        public String isPermaLink {get;set;} 
    }
    
    public class Image {
        public String title {get;set;} 
        public String width {get;set;} 
        public String height {get;set;} 
        public String link {get;set;} 
        public String url {get;set;} 

    }
    
    public class Units {
        public String distance {get;set;} 
        public String pressure {get;set;} 
        public String speed {get;set;} 
        public String temperature {get;set;} 

    }
    
    public class Item {
        public String title {get;set;} 
        public String lat {get;set;} 
        public String long_Z {get;set;} // in json: long
        public String link {get;set;} 
        public String pubDate {get;set;} 
        public Condition condition {get;set;} 
        public List<Forecast> forecast {get;set;} 
        public String description {get;set;} 
        public Guid guid {get;set;} 
        
    }
    
    public class Results {
        public Channel channel {get;set;} 

    }
    
    public class Atmosphere {
        public String humidity {get;set;} 
        public String pressure {get;set;} 
        public String rising {get;set;} 
        public String visibility {get;set;} 

    }
    
    public class Forecast {
        public String code {get;set;} 
        public String date2 {get;set;} 
        public String day {get;set;} 
        public String high {get;set;} 
        public String low {get;set;} 
        public String text {get;set;} 

    }
    
    public class Astronomy {
        public String sunrise {get;set;} 
        public String sunset {get;set;} 
    }
    
    public class Location {
        public String city {get;set;} 
        public String country {get;set;} 
        public String region {get;set;} 

    }

}