Skip to main content

Deam I have written the below code to integration weather details based on the city from openweatherAPI site. it looks everty thing good in Code but I am only getting Normal Temperature value not getting results after that. Can any one look into code and help me sortout.

 

VF Page: 

<apex:page controller="ShowWeatherDetailsController" setup="true" showHeader="true" sidebar="true" tabStyle="Employee__c">

    <apex:sectionHeader title="Weather Details"/>

    <apex:Form >

    <apex:pageBlock >

                <apex:pageBlockSection title="Weather Details by City" columns="2" collapsible="false">

                    <apex:inputtext label="Enter City Name" html-placeholder="Enter City Name" value="{!cityName}"/>

                    <apex:commandButton value="Show Weather Details" action="{!getWeatherDetails}" reRender="pgBlock"/>                

                </apex:pageBlockSection>                

            </apex:pageBlock>

            <apex:pageBlock title="Weather Details" id="pgBlock">

                {!weatherResponseJSON}

                <apex:pageBlockSection title="Weather Details by city" columns="2" collapsible="false">

                    

                    <apex:outputLabel ><b> Norml Temperature:{!NormalTemperature}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Feels Like Temperature:{!feelLikeTemperature}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Minimum Temperature:{!minimumTemperature}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Maximum Temperature:{!maximumTemperature}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Pressure Value:{!pressureValue}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Humidity Value:{!humidityValue}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Sea Level Value:{!seaLevelValue}</b></apex:outputLabel>

                    <apex:outputLabel ><b> Ground Level Value:{!groundLevelValue}</b></apex:outputLabel>

                </apex:pageBlockSection>

            

            </apex:pageBlock>

    </apex:Form>

</apex:page>

 

Apex controller Class: 

 

public class ShowWeatherDetailsController {

    

    Public String cityName{get;set;}

    Public String weatherResponseJSON{get;set;}

    

    Public String NormalTemperature{get;set;}

            Public String feelLikeTemperature{get;set;}

            Public String minimumTemperature{get;set;}

            Public String maximumTemperature{get;set;}

            Public String pressureValue{get;set;}

            Public String humidityValue{get;set;}

            Public String seaLevelValue{get;set;}

    Public String groundLevelValue{get;set;}

      

    Public void getWeatherDetails(){

        

        //prepare REquire Input

        

        String endPointURL = System.Label.WeatherMapAPIEndPoint + cityName + '&appid=' + System.Label.WeatherMapAPIKey;

        

        system.debug('endpoint url results ' +endPointURL );

        

        Http wHttp = new Http();

        

        HttpRequest weatherRequest = new HttpRequest ();

        

        weatherRequest.setMethod('GET');

        weatherRequest.setEndpoint(endPointURL);

        weatherRequest.setTimeout(60000);

        

        HttpResponse weatherRespose = wHttp.send(weatherRequest);

        

        weatherResponseJSON = weatherRespose.getBody();

        

        System.JSONParser weatherParser = System.JSON.createParser(weatherRespose.getBody());

        

        While(weatherParser.nextToken()!= null)

        {

            if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()=='main')

            {

                weatherParser.nextToken();

                

                if(weatherParser.getCurrentToken() == JSONToken.START_OBJECT)

                {

                    weatherParser.nextToken();

                    

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()=='temp')

                    {

                        weatherParser.nextToken();

                        

                        NormalTemperature = (weatherParser.getDecimalValue()-273.15) + ' c';

                        

                         system.debug('NormalTemperature'+ NormalTemperature );

                    }

                   

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'feels_like')

                    {

                        weatherParser.nextToken();

                        

                        feelLikeTemperature = (weatherParser.getDecimalValue()-273.15) + ' c';

                        

                        system.debug('feels like temp'+ feelLikeTemperature );

                    }

                             

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'temp_min')

                    {

                        weatherParser.nextToken();

                        

                        minimumTemperature = (weatherParser.getDecimalValue()-273.15) + ' c';

                        

                    }

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'temp_max')

                    {

                        weatherParser.nextToken();

                        

                        maximumTemperature = (weatherParser.getDecimalValue()-273.15) + ' c';

                        

                    }

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'pressure')

                    { 

                        weatherParser.nextToken();

                        

                        pressureValue = weatherParser.getText();

                        

                    }

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'humidity')

                    { 

                        weatherParser.nextToken();

                        

                        humidityValue = weatherParser.getText();

                    }

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'sea_level')

                    { 

                        weatherParser.nextToken();

                        

                        seaLevelValue = weatherParser.getText();

                    } 

                    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME && weatherParser.getText()== 'grnd_level')

                    { 

                        weatherParser.nextToken();

                        

                        groundLevelValue = weatherParser.getText();

                    } 

                    

                }

            }

        }

    }

    

}

1 réponse
  1. 22 avr. 2023, 22:11

    Hello @Subrahmanyam Kalyanam, it seems each time you start your start or restart your while loop, you test if your next "key node" is "main". It is true only the first time you step into the "main" parent node. After it is not the case anymore.

    While(weatherParser.nextToken()!= null) {

    if(weatherParser.getCurrentToken() == JSONToken.FIELD_NAME

    && weatherParser.getText()=='main')

    Once it is said,  I think you should probably use more something like

    while (weatherParser.nextToken() != JSONToken.END_OBJECT) {

    if (weatherParser.getCurrentToken() == JSONToken.FIELD_NAME) {

    String sNodeKey = weatherParser.getText();

    if (weatherParser.nextToken() != JSONToken.VALUE_NULL) {

    if (sNodeKey == 'temp') {

    Decimal dValue = weatherParser.getDecimalValue();

    NormalTemperature = dValue.format()+' c'; // or equivalent

    } else if (sNodeKey == 'feels_like') {

    Decimal dValue = weatherParser.getDecimalValue();

    feelLikeTemperature = dValue.format()+' c'; // or equivalent

    } else if (sNodeKey == 'humidity') {

    humidityValue = weatherParser.getText();

    } else if (sNodeKey == 'pressure') {

    pressureValue = weatherParser.getText();

    (...) // all the variable nodes to handle

    } else {

    (...) // code to handle if you want to trace a unrecognized node

    }

    }

    }

    }

    PS : I would use number fields first in the wrapper / JSON deserialization part, and then format in string in the last moment, but it is just a matter of point of view :)

     

    Hope it helps

    BR

    Eric

0/9000