I'm using a WeatherTime API.
When I get the weather data from a city via JSON I get the temperature as follows:
clima.setTemperatura((Long) dataNode.get("temperature"))
However, in the API documentation, when I use get ("temperature") it can return a value of "20" or a value of "27.4" in an Object type. This value is in degrees Celsius.
I'm having an exception in java that says (depending) that:
Double can not be converted to Long
The temperatura
attribute with type Long
is in my Clima.java class, I defined it from when I created the class and created the data types in the database. So, I CAN NOT change to String because I will use this data for statistical purposes.
What should I do to make the treatment if it is "20" (Long) or "27.4" does it not cause these exceptions?
EDITION As requested in the comments, I will add more details.
I use the following method to access the API:
@GetMapping("/clima/{idCidade}/agora")
public ResponseEntity<Clima> getClimaAgora(@PathVariable(value = "idCidade") Long idCidade){
JSONObject climateData = new JSONObject();
ClimaTempoAPI ct = new ClimaTempoAPI();
String weatherEndpoint = "/api/v1/weather/locale/"+idCidade+"/current?";
climateData = ct.RequestWeather(weatherEndpoint);
JSONObject dataNode = (JSONObject) climateData.get("data");
Clima clima = new Clima();
clima = setClimateData(climateData, dataNode, clima);
climaRepository.save(clima);
return ResponseEntity.ok().body(clima);
}
The error is occurring in the setClimateData
method which has the following implementation in the same class:
public Clima setClimateData(JSONObject climateData, JSONObject dataNode, Clima clima){
clima.setVelVento((Long) dataNode.get("wind_velocity"));
clima.setDirVento((String) dataNode.get("wind_direction"));
(...)
clima.setTemperatura((Long) dataNode.get("temperature") ); //Erro acontece aqui
}
All other sets
work without any problems. Only in this one of the temperature that gives error, therefore can come as much an integer as decimal.
I tried to use Numeric but it just will not. He says he can not be a type.
I have tried some things. How to convert from Double to Long. I'm trying to use JPA and Hibernate to do this. It's just not what I want ...