1

This issue lead after I solved one issue regarding JSON Deserialization.Now I need to extract JSON using @future method.I retrieve the JSON and try to deserialize it, but it said it does not recognize the GeoCodeResult.class .The class is inner class. The error message is 'Error: Compile Error: Variable does not exist: GeoCodeResult.class' I guess it due to static rule, but what should I do in order to deserialize the object in @future?

@future(callout=true)
  public  static  void updateAddressByGeocode(){

   try{

    String baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:80504'  ;

    HTTPRequest request = new HTTPRequest();              
    request.setEndpoint(baseUrl);
    request.setHeader('Content-Type', 'application/json');
    request.setMethod('GET');
    request.setTimeout(120000);

    HTTP http = new HTTP();
    HTTPResponse  response =  http.send(request);    
    response.getStatusCode();
    response.getBody();

   GeoCodeResult geo =(GeoCodeResult)JSON.deserialize(response.getBody(),GeoCodeResult.class);

   String state ='';
   String city='';

   if(geo.status=='OK'){

         for(Results results : geo.results){

             for( Address_components address :  results.address_components){
                 if(address.types.get(0)=='locality'){
                       city=address.long_name;
                 }
                 else if(address.types.get(0)=='administrative_area_level_1'){
                       state=address.long_name;
                }
             }
      }

         system.debug('city ==='+city + ' , state == '+ state);

   }
   else {
       system.debug(' results === '+geo.status);


   }

    }
 catch(Exception e){
       String errorMessage='Err :: ';
       errorMessage+=e.getMessage();
       system.debug(errorMessage);
 }
}

//inner class
public class GeoCodeResult {
    public List<Results> results;
    public String status;

}

public class Address_components {
    public String long_name;
    public String short_name;
    public List<String> types;
}

public class Results {
    public List<Address_components> address_components;
}
2
  • guess: If its an innerclass you may need to reference it through the outerclass. Or give it it's own file so that it's no longer an innerclass. Commented Jun 24, 2014 at 7:32
  • Your code does not use its "str" argument: the response.getBody() may be an error response so you should use System.debug to output it (and response.getStatusCode() that your code should be checking before you try to deserialise) and work from there. Note that it helps if you post the specific error you get including line numbers and point to the line number in your code.
    – Keith C
    Commented Jun 24, 2014 at 7:43

1 Answer 1

0

I m not sure is this proper way to solve it.But I create new class that contain all JSON object properties plus @future method that do deserialization.The deserialization will done on that class instead of inner class.No more compilation error.Thanks to @Samuel De Rycke for the hint.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .