Advertisement
Guest User

Weather in json

a guest
Sep 16th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. MyJson.java
  2. ===================
  3. package Weather;
  4.  
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.nio.charset.Charset;
  15.  
  16. public class MyJson {
  17.     public final String API_URL ="https://api.darksky.net/forecast/"; //url of the site
  18.     private String apiKey; //api key from the site
  19.     private String GPSlocation; //gps loaction
  20.     private String rawData; //raw data
  21.     private String fullURL; //full url location
  22.     private String cityName;
  23.     JSONObject jsonObject;
  24.  
  25.     public MyJson(String apiKey, String GPSlocation, String cityName) throws IOException, JSONException {
  26.         this.apiKey = apiKey;
  27.         this.GPSlocation = GPSlocation;
  28.         this.cityName = cityName;
  29.         //create a full url for the api....
  30.         this.fullURL = API_URL+this.apiKey+"/"+this.GPSlocation;
  31.         //readJsonFromURL();
  32.     }
  33.  
  34.     private JSONObject readJsonFromURL() throws IOException, JSONException {
  35.         //openm a connection to the internet with input stream
  36.         InputStream is = new URL(this.fullURL).openStream();
  37.         //create a buffer for collection all the data
  38.         BufferedReader buf = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  39.         //read all data and append it to one happy String
  40.         String jsonText = readAll(buf);
  41.         //convert to json object, so we can use the json power.
  42.         jsonObject = new JSONObject(jsonText);
  43.         //return the json object
  44.         return jsonObject;
  45.     }
  46.  
  47.     private String readAll(BufferedReader rd) throws IOException{
  48.         //create a string builder
  49.         StringBuilder sb = new StringBuilder();
  50.         //create a char pointer
  51.         int cp;
  52.         while ((cp=rd.read()) != -1) {
  53.             //append the char to our String builder
  54.             sb.append((char) cp);
  55.         }
  56.         //return the result
  57.         rawData=sb.toString();
  58.         return sb.toString();
  59.     }
  60.  
  61.     //get raw data for testing our communication
  62.     public String getRaw(){
  63.         return rawData;
  64.     }
  65.  
  66.     private int getClouds(JSONObject cur) throws IOException, JSONException {
  67.         int cloud =(int)(cur.getDouble("cloudCover")*100);
  68.         return cloud;
  69.     }
  70.  
  71.     private int getTemp(JSONObject cur) throws IOException, JSONException{
  72.         int temp=(int)cur.getDouble("temperature");
  73.         return (int)((temp-32) /1.8);
  74.     }
  75.  
  76.     public boolean openBoiler(int tolerance,int minTemp) throws IOException, JSONException {
  77.         JSONObject myData = readJsonFromURL();
  78.         JSONObject currently = myData.getJSONObject("currently");
  79.  
  80.         return getClouds(currently)>tolerance && getTemp(currently)<minTemp;
  81.     }
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88. Tester.java
  89. =================
  90. package Weather;
  91.  
  92. import org.json.JSONException;
  93.  
  94. import java.io.IOException;
  95.  
  96. public class Tester {
  97.     public static void main(String[] args) throws IOException, JSONException {
  98.         MyJson myData = new MyJson("0e3a4cef498bc2ad18a97e1817c79e87","50.4019514,30.6727719","Ramat-Gan");
  99.         //System.out.println(myData.getRaw());
  100.         System.out.println("Getting data from the web.");
  101.         //get clouds
  102.         System.out.println("Need to open boiler:"+myData.openBoiler(30));
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement