Advertisement
yanivtamari

Weather

Jul 14th, 2019
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. WeatherJson.java
  2. ===================
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.net.URL;
  11. import java.nio.charset.Charset;
  12.  
  13. public class WeatherJason {
  14.     private final String apiURL = "https://api.darksky.net/forecast/";
  15.     private String apiKEY;
  16.     private String gpsLocation;
  17.     private String rawData;
  18.     private String fullURL;
  19.     private String cityname;
  20.     private JSONObject json;
  21.  
  22.     public String getCityname() {
  23.         return cityname;
  24.     }
  25.  
  26.     public WeatherJason(String apiKEY, String gpsLocation,String cityname) throws IOException, JSONException {
  27.         this.apiKEY = apiKEY;
  28.         this.gpsLocation = gpsLocation;
  29.         this.cityname=cityname;
  30.         this.fullURL = apiURL + apiKEY + "/" + gpsLocation;
  31.         readJsonFromURL( );
  32.     }
  33.  
  34.     public JSONObject getRawData() throws IOException, JSONException {
  35.         JSONObject json = readJsonFromURL( );
  36.         return json;
  37.     }
  38.  
  39.     private JSONObject readJsonFromURL() throws IOException, JSONException {
  40.             InputStream is = new URL(this.fullURL).openStream( );
  41.             try {
  42.                 BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  43.                 String jsonText = readAll(rd);
  44.                 json = new JSONObject(jsonText);
  45.                 return json;
  46.             } finally {
  47.                 is.close( );
  48.             }
  49.     }
  50.  
  51.     private String readAll(BufferedReader rd) throws IOException {
  52.         StringBuilder sb = new StringBuilder( );
  53.         int cp;
  54.         while ((cp = rd.read( )) != -1) {
  55.             sb.append((char) cp);
  56.         }
  57.         return sb.toString( );
  58.     }
  59.  
  60.     private String getRaw() {
  61.         return rawData;
  62.     }
  63.  
  64.     public JSONObject getCurrent() throws JSONException {
  65.         return (JSONObject) json.get("currently");
  66.     }
  67.  
  68.     public double gethumidity() throws JSONException {
  69.         JSONObject current = (JSONObject) json.get("currently");
  70.         double humidity = (int) (current.getDouble("humidity")*100);
  71.         return humidity;
  72.     }
  73.  
  74.     public double getpressure() throws JSONException {
  75.         JSONObject current = (JSONObject) json.get("currently");
  76.         double pressure = (int) (current.getDouble("pressure"));
  77.         return pressure;
  78.     }
  79.  
  80.     public double getwindBearing() throws JSONException {
  81.         JSONObject current = (JSONObject) json.get("currently");
  82.         double windBearing = (current.getDouble("windBearing"));
  83.         return windBearing;
  84.     }
  85.  
  86.     public double getwindSpeed() throws JSONException {
  87.         JSONObject current = (JSONObject) json.get("currently");
  88.         double windSpeed = (int) (current.getDouble("windSpeed"));
  89.         return windSpeed;
  90.     }
  91.  
  92.     public double getcloudCover() throws JSONException {
  93.         JSONObject current = (JSONObject) json.get("currently");
  94.         double cloudCover = (int) (current.getDouble("cloudCover"));
  95.         return cloudCover;
  96.     }
  97.  
  98.     public double getvisibility() throws JSONException {
  99.         JSONObject current = (JSONObject) json.get("currently");
  100.         double visibility = (int) (current.getDouble("visibility"));
  101.         return visibility;
  102.     }
  103.     public void setGpsLocation(String gpsLocation) {
  104.         this.gpsLocation = gpsLocation;
  105.     }
  106. }
  107.  
  108.  
  109. Main.java
  110. ======================
  111. import org.json.JSONException;
  112.  
  113. import java.io.IOException;
  114. import java.util.Scanner;
  115.  
  116. public class Main {
  117.  
  118.     public static void main(String[] args) throws IOException, JSONException {
  119.         Scanner s = new Scanner(System.in);
  120.         // System.out.println("The Weather In Yoqneam Illit is: ");
  121.         String apiKey = "9fac30f67bf26ec980ed39506e0e61c1";
  122.         System.out.println("To Weather in Los Angeles Press 0\nTo Weather in Yoqneam Press 1\nTo Weather in Haifa Press 2");
  123.         //String Gpsloc = "";
  124.         WeatherJason[] City = new WeatherJason[3];
  125.         City[0] = new WeatherJason(apiKey, "34.0207305,-118.6919155", "Los Angeles");
  126.         City[1] = new WeatherJason(apiKey, "32.6554029,35.0708915", "Yoqneam");
  127.         City[2] = new WeatherJason(apiKey, "32.7996897,34.9817566", "Haifa");
  128.         for (int i=0;i<3;i++) {
  129.             s.nextInt();
  130.             System.out.println("The Weather In " + City[i].getCityname( ) + ":");
  131.             System.out.println("Humidity currently: " + City[i].gethumidity( ) + "%");
  132.             System.out.println("Pressure currently: " + City[i].getpressure( ) + "%");
  133.             System.out.println("WindBearing currently: " + City[i].getwindBearing( ) + "%");
  134.             System.out.println("WindSpeed currently: " + City[i].getwindSpeed( ) + "%");
  135.             System.out.println("CloudCover currently: " + City[i].getcloudCover( ) + "%");
  136.             System.out.println("Visibility currently: " + City[i].getvisibility( ) + "%");
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement