· 6 years ago · Sep 16, 2019, 05:30 PM
1MyJson.java
2===================
3package Weather;
4
5import org.json.JSONException;
6import org.json.JSONObject;
7
8import java.io.BufferedReader;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.InputStreamReader;
12import java.net.MalformedURLException;
13import java.net.URL;
14import java.nio.charset.Charset;
15
16public 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
88Tester.java
89=================
90package Weather;
91
92import org.json.JSONException;
93
94import java.io.IOException;
95
96public 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}