Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WeatherJson.java
- ===================
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.nio.charset.Charset;
- public class WeatherJason {
- private final String apiURL = "https://api.darksky.net/forecast/";
- private String apiKEY;
- private String gpsLocation;
- private String rawData;
- private String fullURL;
- private String cityname;
- private JSONObject json;
- public String getCityname() {
- return cityname;
- }
- public WeatherJason(String apiKEY, String gpsLocation,String cityname) throws IOException, JSONException {
- this.apiKEY = apiKEY;
- this.gpsLocation = gpsLocation;
- this.cityname=cityname;
- this.fullURL = apiURL + apiKEY + "/" + gpsLocation;
- readJsonFromURL( );
- }
- public JSONObject getRawData() throws IOException, JSONException {
- JSONObject json = readJsonFromURL( );
- return json;
- }
- private JSONObject readJsonFromURL() throws IOException, JSONException {
- InputStream is = new URL(this.fullURL).openStream( );
- try {
- BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
- String jsonText = readAll(rd);
- json = new JSONObject(jsonText);
- return json;
- } finally {
- is.close( );
- }
- }
- private String readAll(BufferedReader rd) throws IOException {
- StringBuilder sb = new StringBuilder( );
- int cp;
- while ((cp = rd.read( )) != -1) {
- sb.append((char) cp);
- }
- return sb.toString( );
- }
- private String getRaw() {
- return rawData;
- }
- public JSONObject getCurrent() throws JSONException {
- return (JSONObject) json.get("currently");
- }
- public double gethumidity() throws JSONException {
- JSONObject current = (JSONObject) json.get("currently");
- double humidity = (int) (current.getDouble("humidity")*100);
- return humidity;
- }
- public double getpressure() throws JSONException {
- JSONObject current = (JSONObject) json.get("currently");
- double pressure = (int) (current.getDouble("pressure"));
- return pressure;
- }
- public double getwindBearing() throws JSONException {
- JSONObject current = (JSONObject) json.get("currently");
- double windBearing = (current.getDouble("windBearing"));
- return windBearing;
- }
- public double getwindSpeed() throws JSONException {
- JSONObject current = (JSONObject) json.get("currently");
- double windSpeed = (int) (current.getDouble("windSpeed"));
- return windSpeed;
- }
- public double getcloudCover() throws JSONException {
- JSONObject current = (JSONObject) json.get("currently");
- double cloudCover = (int) (current.getDouble("cloudCover"));
- return cloudCover;
- }
- public double getvisibility() throws JSONException {
- JSONObject current = (JSONObject) json.get("currently");
- double visibility = (int) (current.getDouble("visibility"));
- return visibility;
- }
- public void setGpsLocation(String gpsLocation) {
- this.gpsLocation = gpsLocation;
- }
- }
- Main.java
- ======================
- import org.json.JSONException;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws IOException, JSONException {
- Scanner s = new Scanner(System.in);
- // System.out.println("The Weather In Yoqneam Illit is: ");
- String apiKey = "9fac30f67bf26ec980ed39506e0e61c1";
- System.out.println("To Weather in Los Angeles Press 0\nTo Weather in Yoqneam Press 1\nTo Weather in Haifa Press 2");
- //String Gpsloc = "";
- WeatherJason[] City = new WeatherJason[3];
- City[0] = new WeatherJason(apiKey, "34.0207305,-118.6919155", "Los Angeles");
- City[1] = new WeatherJason(apiKey, "32.6554029,35.0708915", "Yoqneam");
- City[2] = new WeatherJason(apiKey, "32.7996897,34.9817566", "Haifa");
- for (int i=0;i<3;i++) {
- s.nextInt();
- System.out.println("The Weather In " + City[i].getCityname( ) + ":");
- System.out.println("Humidity currently: " + City[i].gethumidity( ) + "%");
- System.out.println("Pressure currently: " + City[i].getpressure( ) + "%");
- System.out.println("WindBearing currently: " + City[i].getwindBearing( ) + "%");
- System.out.println("WindSpeed currently: " + City[i].getwindSpeed( ) + "%");
- System.out.println("CloudCover currently: " + City[i].getcloudCover( ) + "%");
- System.out.println("Visibility currently: " + City[i].getvisibility( ) + "%");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement