Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MyJson.java
- ===================
- package Weather;
- 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.MalformedURLException;
- import java.net.URL;
- import java.nio.charset.Charset;
- public class MyJson {
- public final String API_URL ="https://api.darksky.net/forecast/"; //url of the site
- private String apiKey; //api key from the site
- private String GPSlocation; //gps loaction
- private String rawData; //raw data
- private String fullURL; //full url location
- private String cityName;
- JSONObject jsonObject;
- public MyJson(String apiKey, String GPSlocation, String cityName) throws IOException, JSONException {
- this.apiKey = apiKey;
- this.GPSlocation = GPSlocation;
- this.cityName = cityName;
- //create a full url for the api....
- this.fullURL = API_URL+this.apiKey+"/"+this.GPSlocation;
- //readJsonFromURL();
- }
- private JSONObject readJsonFromURL() throws IOException, JSONException {
- //openm a connection to the internet with input stream
- InputStream is = new URL(this.fullURL).openStream();
- //create a buffer for collection all the data
- BufferedReader buf = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
- //read all data and append it to one happy String
- String jsonText = readAll(buf);
- //convert to json object, so we can use the json power.
- jsonObject = new JSONObject(jsonText);
- //return the json object
- return jsonObject;
- }
- private String readAll(BufferedReader rd) throws IOException{
- //create a string builder
- StringBuilder sb = new StringBuilder();
- //create a char pointer
- int cp;
- while ((cp=rd.read()) != -1) {
- //append the char to our String builder
- sb.append((char) cp);
- }
- //return the result
- rawData=sb.toString();
- return sb.toString();
- }
- //get raw data for testing our communication
- public String getRaw(){
- return rawData;
- }
- private int getClouds(JSONObject cur) throws IOException, JSONException {
- int cloud =(int)(cur.getDouble("cloudCover")*100);
- return cloud;
- }
- private int getTemp(JSONObject cur) throws IOException, JSONException{
- int temp=(int)cur.getDouble("temperature");
- return (int)((temp-32) /1.8);
- }
- public boolean openBoiler(int tolerance,int minTemp) throws IOException, JSONException {
- JSONObject myData = readJsonFromURL();
- JSONObject currently = myData.getJSONObject("currently");
- return getClouds(currently)>tolerance && getTemp(currently)<minTemp;
- }
- }
- Tester.java
- =================
- package Weather;
- import org.json.JSONException;
- import java.io.IOException;
- public class Tester {
- public static void main(String[] args) throws IOException, JSONException {
- MyJson myData = new MyJson("0e3a4cef498bc2ad18a97e1817c79e87","50.4019514,30.6727719","Ramat-Gan");
- //System.out.println(myData.getRaw());
- System.out.println("Getting data from the web.");
- //get clouds
- System.out.println("Need to open boiler:"+myData.openBoiler(30));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement