0

enter image description hereapex class:

public with sharing class AccountWeatherController {
    public String response { get; set; }
    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 static  void searchEventr() {
      String city;
       String temp;
       String pressure;
       String temp_min;
       String temp_max ;
       String humidity;
        String apiKey = '1a94a773609a33fc2852b07db7d439af';
        String requestEndpoint = 'http://api.openweathermap.org/data/2.5/weather';
        requestEndpoint += '?q=' +City;
        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,string> results = (Map<string,string>) JSON.deserializeUntyped(response.getBody());
           city = String.valueOf(results.get('name'));
           //Map<string,string> mainResults = (Map<string,string>)(results.get('main'));
           temp = String.valueOf(Results.get('temp'));
           pressure = String.valueOf(Results.get('pressure'));
            humidity = String.valueOf(Results.get('humidity')); 
            temp_min = String.valueOf(Results.get('temp_min')); 
            temp_max = String.valueOf(Results.get('temp_max'));

        } else {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error retrieving the weather information.');
           ApexPages.addMessage(myMsg);
        }
    }
     public PageReference search() {
        searchEventr();
        return null;
    }
}

vf page:

<apex:page controller="AccountWeatherController" >
   <apex:form >
       <apex:inputText value="{!CITY}"/>
       <apex:commandButton value="click" action="{!searchEventr}"/>  
       <apex:outputtext value="{!response}"/>            
    </apex:form>        
</apex:page>
2

You need to actually output the data by placing the merge fields in to the page:

<apex:page controller="AccountWeatherController" >
    <apex:form >
        <apex:inputText value="{!CITY}"/>
        <apex:commandButton value="click" action="{!searchEventr}"/>  
        <apex:pageBlockSection>
            <apex:outputText value="{!pressure}" label="Pressure" />
            <apex:outputText value="{!humidity}" label="Humidity" />
            <apex:outputText value="{!temp_min}" label="Min Temp" />
            <apex:outputText value="{!temp_max}" label="Max Temp" />
        </apex:pageBlockSection>
    </apex:form>
</apex:page>

The response variable had never been populated, which is why you did not see anything.

You also need to save the data to the instance variables. This means you need to remove the static keyword and the local instance variables.

Here's a modified version:

public void searchEventr() {
    String apiKey = '1a94a773609a33fc2852b07db7d439af';
    String requestEndpoint = 'http://api.openweathermap.org/data/2.5/weather';
    requestEndpoint += '?q=' +City;
    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,string> results = (Map<string,string>) JSON.deserializeUntyped(response.getBody());
       city = String.valueOf(results.get('name'));
       //Map<string,string> mainResults = (Map<string,string>)(results.get('main'));
       temp = String.valueOf(Results.get('temp'));
       pressure = String.valueOf(Results.get('pressure'));
        humidity = String.valueOf(Results.get('humidity')); 
        temp_min = String.valueOf(Results.get('temp_min')); 
        temp_max = String.valueOf(Results.get('temp_max'));

    } else {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error retrieving the weather information.');
       ApexPages.addMessage(myMsg);
    }
}

I have not tested this code, so additional changes may be necessary.

  • thanks for your reply still it doesn't displaying anything here i am sharing i am in an urgency.please help me out. – lucky Feb 23 at 14:08
  • @lucky I don't know what to say to "doesn't displaying anything." You may need to do some debugging. As far as I can tell, it should work with the suggested changes. – sfdcfox Feb 23 at 15:23
  • here i am sharing my debug log file – lucky Feb 26 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.