Last active
August 29, 2015 14:05
-
-
Save EDCHRONO-GIT/7e894c4f95bbbb3795b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="RestCalloutDemo"> | |
<apex:form > | |
Select City: <apex:selectList id="chooseCity" value="{!city}" size="1"> | |
<apex:selectOption itemValue="London" itemLabel="London"/> | |
<apex:selectOption itemValue="Delhi" itemLabel="New Delhi"/> | |
<apex:selectOption itemValue="Sydney" itemLabel="Sydney"/> | |
<apex:actionSupport event="onchange" action="{!requiredWeatherJSON}" reRender="weatherpanel"/> | |
</apex:selectList> | |
</apex:form> | |
<apex:pageBlock title="Weather for:" id="weatherpanel"> | |
<apex:pageBlockSection collapsible="false" title="{!city}" > | |
<apex:outputText label="Longitude" value="{!ww.coord.lon}"></apex:outputText> | |
<apex:outputText label="Sunrise" value="{!ww.sys.sunrise}"></apex:outputText> | |
<apex:outputText label="Latitude" value="{!ww.coord.lon}"></apex:outputText> | |
<apex:outputText label="Sunset" value="{!ww.sys.sunset}"></apex:outputText> | |
<apex:outputText label="weather Description" value="{!ww.weather[0].description}"></apex:outputText> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
</apex:page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RestCalloutDemo { | |
public string weatherJSON; | |
public string city{get;set;} | |
public WeatherWrapper ww{get;set;} | |
public pageReference requiredWeatherJSON() | |
{ | |
//Step 1: Create a http request object | |
Http httpProtocol = new Http(); | |
HttpRequest request = new HttpRequest(); | |
//End point will be your rest service URL | |
String endpoint = 'http://api.openweathermap.org/data/2.5/weather?q='+city; | |
request.setEndPoint(endpoint); | |
request.setMethod('GET'); | |
//step 2:get response | |
HttpResponse response = httpProtocol.send(request); | |
weatherJSON = response.getBody(); | |
//Step 3:parse the received JSON | |
//please refer my post "PARSING JSON IN SALESFORCE AND SHOWING DATA IN A VISUALFORCE TABLE" for more info on parsing | |
JSONParser parser = JSON.createParser(weatherJSON); | |
while (parser.nextToken() != null) { | |
if (parser.getCurrentToken() == JSONToken.START_OBJECT) { | |
// Read entire JSON object, including its array of items. | |
ww = ( WeatherWrapper)parser.readValueAs(WeatherWrapper.class); | |
} | |
} | |
return null; | |
} | |
//This will be the main wrapper for JSON | |
public class WeatherWrapper | |
{ | |
//Write a inner class for each JSON object and declare it in the main class as shown below. | |
public coordinates coord{get;set;} | |
public sys sys{get;set;} | |
//if we have a JSON array we have to declare a list of that particular inner class type | |
public List<weather> weather{get;set;} | |
} | |
public class coordinates | |
{ | |
public String lon{get;set;} | |
public String lat{get;set;} | |
} | |
public class sys | |
{ | |
public String country{get;set;} | |
public String sunrise{get;set;} | |
public String sunset{get;set;} | |
} | |
public class weather | |
{ | |
public String id{get;set;} | |
public String main{get;set;} | |
public String description{get;set;} | |
public String icon{get;set;} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment