I work at a weather app. I use a free api to get the weather information. When I try yo run this class i get "null" Why?You can see the json code in the url. It should return "Romania" but it returns "null" and I don't know why.
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.apache.commons.io.IOUtils;
import org.json.simple.*;
import org.json.simple.parser.ParseException;
public class Weather {
public static URL url;
public void getWeather() throws IOException, ParseException {
url = new URL("http://api.openweathermap.org/data/2.5/weatherq=drobeta,romania");
Scanner scan = new Scanner(url.openStream());
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();
System.out.println(str);
String genreJson = IOUtils.toString(url);
JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);
// get the title
System.out.println(genreJsonObject.get("country"));
}
}