+ Start a Discussion
Sachin Bhalerao 17Sachin Bhalerao 17 

REST API UsingWeather Example ?

Dear Team ,

Greetings !!!

I am performing REST API UsingWeather Example . M refering this website https://focusonforce.com/integration-and-data-loading/rest-api-using-weather-example/ . On my visual force m experiencing error . Plz have a look on my Code and snapshot .  I enable my API moreover configuration settings also i made . Plz help me to solve this issue .

User-added image
APEX Class :

public with sharing class testopenweather {

	public String city {get;set;}
	public String temp {get;set;}
	public String pressure {get;set;}
	public String humidity {get;set;}
	public String temp_min {get;set;}
	public String temp_max {get;set;}

	public testopenweather(ApexPages.StandardController stdController) {
		Account account = (Account)stdController.getRecord();
		account = [SELECT Id, ShippingCity FROM Account WHERE Id =:account.Id];
		//account = [SELECT Id, ShippingCity FROM Account WHERE Id ='0012v00002pNKoyAAG'];
        
		String accountCity = account.ShippingCity;
		String apiKey = '0284633ceb975c6164fa90f016d87e02';

		String requestEndpoint = 'http://api.openweather.org/data/2.5/weather';
		requestEndpoint += '?q=' + accountCity;
		requestEndpoint += '&units=metric';
		requestEndpoint += '&APPID=' + apiKey;
		
		Http http = new Http();
		HttpRequest request = new HttpRequest();
		request.setEndpoint(requestEndpoint);
		request.setMethod('GET');
		HttpResponse response = http.send(request);

		// If the request is successful, parse the JSON response.
		if (response.getStatusCode() == 200) {

		   // Deserialize the JSON string into collections of primitive data types.
		  Map<string,object> results=( Map<string,object>)JSON.deserializeUntyped(response.getBody());
           // city=string.valueof(result.get('name'));
           city = String.valueOf(results.get('name')); 
             Map<string,object> mainresults=( Map<string,object>)(results.get('main'));
            temp = string.valueOf(mainresults.get('temp'));
             pressure = string.valueOf(mainresults.get(' pressure'));
             humidity = string.valueOf(mainresults.get('humidity'));
             temp_min = string.valueOf(mainresults.get('temp_min'));
             temp_max = string.valueOf(mainresults.get('temp_max'));
            
        }
        else {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Tere was an error');
            ApexPages.addMessage(myMsg);
            
        }
    }
}

Visual Force :

<apex:page standardController="Account" extensions="testopenweather" showHeader="false" sidebar="false">
    <apex:pageBlock title="{!city} Weather">
		<apex:pageBlockSection>

			<apex:pageMessages/>

			<apex:outputText label="Temperature" value="{!temp}"/>
			<apex:outputText label="Pressure" value="{!pressure}"/>
			<apex:outputText label="Humidity" value="{!humidity}"/>
			<apex:outputText label="Minimum Temperature" value="{!temp_min}"/>
			<apex:outputText label="Maximum Temperature" value="{!temp_max}"/>

		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:page>
Thanks & Regards
Sachin Bhalerao
 
Prady01Prady01
Hi there, I dont think this external URL you are trying to hit is working. Its possible your status code is as below.

[Status=Service Unavailable, StatusCode=503]

Hope this helps!
Prady01
Sachin Bhalerao 17Sachin Bhalerao 17
Dear Prady ,

Thank you for your reply . I changed status code to 503 but still API is not working . Plzhave a look on attached imageUser-added image

Thanks & Regards
Sachin Bhalerao