You need to sign in to do that
Don't have an account?
Nitin sharma 425
URL issue with Json
Hi All,
String Api='6e8e6478bc45d53ef7e693299ed86f13';
//https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+googleApiKey+'&
city=EncodingUtil.urlEncode(city,'UTF-8');
String MapUrl='https://api.openweathermap.org/data/2.5/weather?q='+city+'&appid='+Api+'';
I am having diffuclty in constructing proprer URL for the callout ..I have listed Api Key but I am getting an error Unauthorised End Point URL.
Can somebody help?
String Api='6e8e6478bc45d53ef7e693299ed86f13';
//https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+googleApiKey+'&
city=EncodingUtil.urlEncode(city,'UTF-8');
String MapUrl='https://api.openweathermap.org/data/2.5/weather?q='+city+'&appid='+Api+'';
I am having diffuclty in constructing proprer URL for the callout ..I have listed Api Key but I am getting an error Unauthorised End Point URL.
Can somebody help?
public class CallWeatherAPI implements Database.Batchable<sobject>,Database.Stateful,Database.AllowsCallouts
{
public class JSON2Apex
{
public Main main {get;set;}
public JSON2Apex(JSONParser parser)
{
while (parser.nextToken() != System.JSONToken.END_OBJECT)
{
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
{
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
{
if (text == 'main')
{
main = new Main(parser);
}
}
}
}
}
}
public class Main {
public Double temp {get;set;}
public Integer feels_like {get;set;}
public Double temp_min {get;set;}
public Double temp_max {get;set;}
public Integer pressure {get;set;}
public Integer humidity {get;set;}
public Main(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'temp') {
temp = parser.getDoubleValue();
} else if (text == 'feels_like') {
feels_like = parser.getIntegerValue();
} else if (text == 'temp_min') {
temp_min = parser.getDoubleValue();
} else if (text == 'temp_max') {
temp_max = parser.getDoubleValue();
} else if (text == 'pressure') {
pressure = parser.getIntegerValue();
} else if (text == 'humidity') {
humidity = parser.getIntegerValue();
} else {
System.debug(LoggingLevel.WARN, 'Main consuming unrecognized property: '+text);
//consumeObject(parser);
}
}
}
}
}
}
public Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator([Select Id,MailingCity from Contact where Mailingcountry!=null and MailingCountry='USA' limit 10]);
}
Public void execute(Database.BatchableContext BC, List<Contact> USContacts)
{
List<Contact>conTemperature=new list<contact>();
Double lon;
Double Lat;
String City='';
Map<Id,Contact>MapofContactIdtoRecords=new map<id,contact>();
For(Contact con:USContacts)
{
if(con.mailingcity!=null)
{
MapofContactIdtoRecords.put(con.id,con);
city=con.mailingcity;
}
String Api='b39b65f767a7ac40e705d71e34e11a91';
city=EncodingUtil.urlEncode(city,'UTF-8');
String MapUrl='http://api.openweathermap.org/data/2.5/weather?q='+city+'&appid='+Api;
Http http=new http();
HttpRequest req= new HttpRequest();
req.setEndPoint(MapUrl);
Req.setMethod('GET');
req.setTimeout(10000);
HttpResponse response=Http.send(req);
//JSONParser parser = JSON.createParser(response.getBody());
Main n = (Main)JSON.deserialize(response.getbody(),main.class);
//contact c=new contact();
con.Temp_Max__c=n.temp_max;
System.Debug('The TemP max is'+n.temp_max);
con.Temp_Min__c=n.temp_min;
System.Debug('The TemP min is'+n.temp_min);
con.Temperatire__c=n.temp;
conTemperature.add(con);
}
//update conTemperature;
}
Public Void Finish(Database.batchableContext Bc)
{
}
}
Main n = (Main)JSON.deserialize(response.getbody(),main.class);
//contact c=new contact();
con.Temp_Max__c=n.temp_max;
System.Debug('The TemP max is'+n.temp_max);
con.Temp_Min__c=n.temp_min;
System.Debug('The TemP min is'+n.temp_min);
con.Temperatire__c=n.temp;
conTemperature.add(con);
}
I have a batch class,2 inner clases inside batch class..The below line are giving me null values.When I debug the variables which are part of the main inner class.I am getting only Null values being passed from the main Variable to the contact object variable.Can Somebody help?
Main n = (Main)JSON.deserialize(response.getbody(),main.class);
//contact c=new contact();
con.Temp_Max__c=n.temp_max;
System.Debug('The TemP max is'+n.temp_max);
con.Temp_Min__c=n.temp_min;
System.Debug('The TemP min is'+n.temp_min);
con.Temperatire__c=n.temp;
conTemperature.add(con);
}
This error is occuring because , org does not find it as a secure Url.
Anytime you want to call an external URL from Salesforce, you need to set the URL as a safe remote site.
To set the URL as a safe remote site follow these steps.
-> Go To Org SETUP
-> Serach Remote Site Setting
-> Click on New Remote Site.
-> Fill Remote Site Name and Remote site URL.
-> Activate it and click on save.
If you find this helpful please mark it as a best answer.
Thank and Regards,
Suraj Tripathi