package mine;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class Js {
public void getWeather(String cityname) {
URL url;
try {
// Connect to URL
url = new URL("http://restapi.demoqa.com/utilities/weather/city/" + cityname);
URLConnection uc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
// Create Json Object to parse
JSONObject obj = (JSONObject) JSONValue.parseWithException(br);
// Get data by Key
cityname = (String) obj.get("City");
String temp = (String) obj.get("Temperature");
String humid = (String) obj.get("Humidity");
String windspeed = (String) obj.get("WindSpeed");
String winddirect = (String) obj.get("WindDirectionDegree");
System.out.println("City: " + cityname);
System.out.println("Tempreture: " + temp);
System.out.println("Humidity: " + humid);
System.out.println("WindSpeed: " + windspeed);
System.out.println("WindDirectionDegree: " + winddirect);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Js j = new Js();
j.getWeather("Vasco");
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class Js {
public void getWeather(String cityname) {
URL url;
try {
// Connect to URL
url = new URL("http://restapi.demoqa.com/utilities/weather/city/" + cityname);
URLConnection uc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
// Create Json Object to parse
JSONObject obj = (JSONObject) JSONValue.parseWithException(br);
// Get data by Key
cityname = (String) obj.get("City");
String temp = (String) obj.get("Temperature");
String humid = (String) obj.get("Humidity");
String windspeed = (String) obj.get("WindSpeed");
String winddirect = (String) obj.get("WindDirectionDegree");
System.out.println("City: " + cityname);
System.out.println("Tempreture: " + temp);
System.out.println("Humidity: " + humid);
System.out.println("WindSpeed: " + windspeed);
System.out.println("WindDirectionDegree: " + winddirect);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Js j = new Js();
j.getWeather("Vasco");
}
}
No comments:
Post a Comment