I'm trying to make a simple weather app. I'm using this API for it. on that same page is a list of parameters in the JSON response. I can search for all the parameters and get a response except for the 'weather' parameter. every time I try that I get an
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 path $[0]
error. I'm not sure what's causing it. here's my code.
public class Weather {
private String city;
private OWM owm = new OWM("8984d739fa91d7031fff0e84a3d2c520");
private CurrentWeather currentWeather;
private String weather;
private Clouds cloud;
public Weather() throws APIException {
String API_KEY = "8984d739fa91d7031fff0e84a3d2c520";
String Location = "Brooklyn";
String urlString = "http://api.openweathermap.org/data/2.5/weather?q=" + Location
+ "&appid=" + API_KEY + "&units=imperial";
try {
StringBuilder result = new StringBuilder();
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null){
result.append(line);
}
rd.close();
System.out.println(result);
Map<String, Object> respMap = jsonToMap(result.toString());
Map<String, Object> mainMap = jsonToMap(respMap.get("main").toString());
Map<String, Object> windMap = jsonToMap(respMap.get("wind").toString());
Map<String, Object> cloudsMap = jsonToMap(respMap.get("weather").toString());
// error is here
System.out.println("Current Temperature: " + mainMap.get("temp"));
System.out.println("current humidity " + mainMap.get("humidity"));
System.out.println("clouds " + respMap.get("description"));
// System.out.println("weather conditions: " + cloudsMap.get("main"));
// this always returns null
System.out.println("wind speeds " + windMap.get("speed"));
System.out.println("wind angle: " + windMap.get("deg"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, Object> jsonToMap(String str){
Map<String, Object> map = new Gson().fromJson(
str, new TypeToken<HashMap<String, Object>>() {}.getType()
);
return map;
}
public String getCityName() {
return cityName;
}
public int getCurrentWeather() throws APIException {
owm.setUnit(OWM.Unit.IMPERIAL);
currentWeather = owm.currentWeatherByCityName(this.cityName);
return (int) Math.round(currentWeather.getMainData().getTemp());
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public void setZipCode(String zipCode){
this.zipCode = zipCode;
}
public String getZipCode(){
return this.zipCode;
}
public String getWeather(){
return this.weather;
}
}
I'm not really sure why that one parameter isn't working. I can search anything else fine, so why can't I search the weather parameter?
Edit:
I'm doing this in my try/catch statement. it's giving me a NullPointerException
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.connect();
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) conn.getContent()));
JsonObject rootObj = root.getAsJsonObject();
String description = rootObj.get("weather").getAsString();
// name of the array in the json
System.out.println(description);
The best way to parse the Json here is Gson Library Here first you can create pojo class of response you getting ,after that just pass the response String and get your output.
Instead of blindly considering Hashmap for keyvalues, consider creating pojos, and then parse it. Create all PoJo for json.
Main.java
Example.java (This pojo is for your json schema)
Coord.java
Weather.java
Main.java
Wind.java
Clouds.java
Sys.java
Map<String, Object> cloudsMap = jsonToMap(respMap.get("weather").toString());
This is the problem. You're trying to parse the
weather
property of the response as an object (a Map) but it's actually an array.