Compare commits

..

No commits in common. "c2b24cc896456421408341f2b0c6378c3d27a31d" and "899a118769839fe2b9a1eee19ec00e0dc2803aa9" have entirely different histories.

4 changed files with 46 additions and 47 deletions

View File

@ -1,7 +1,6 @@
package sh.adb.sensorCommunityAPI; package sh.adb.sensorCommunityAPI;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
@ -20,9 +19,9 @@ public class APIPaser {
public APIPaser(String urlString) throws MalformedURLException { public APIPaser(String urlString) throws MalformedURLException {
this.url = urlString; this.url = urlString;
} }
public JSONArray getJSONObject() throws IOException, ParseException { public Object getJSON() throws IOException, ParseException {
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
return (JSONArray) parser.parse(this.getRequest()); return parser.parse(this.getRequest());
} }
public String getRequest() throws IOException { public String getRequest() throws IOException {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

View File

@ -1,21 +0,0 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import java.io.FileWriter;
import java.io.IOException;
public class DataStore {
public boolean storeJSON(String path, JSONObject json) throws IOException {
FileWriter fw = new FileWriter(path);
try {
System.out.println(json.toString());
fw.write(json.toString());
fw.close();
return true;
}catch (IOException e){
System.out.println("error wile soring data to: " + path);
}
return false;
}
}

View File

@ -1,7 +1,5 @@
package sh.adb.sensorCommunityAPI; package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import java.io.IOException; import java.io.IOException;
@ -18,27 +16,7 @@ import java.io.IOException;
public class Main { public class Main {
public static void main(String[] args) throws IOException, ParseException { public static void main(String[] args) throws IOException, ParseException {
String basePath = "./data";
APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/"); APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/");
JSONArray APIData = api.getJSONObject(); api.getJSON();
JSONObject entry = (JSONObject) APIData.get(0);
System.out.println("entry => ");
System.out.println(entry);
//get country
String country = ((JSONObject) entry.get("location")).get("country").toString();
//get sensorID
String sensorID = String.valueOf(
((JSONObject) entry.get("sensor")).get("id")
);
//get timestamp
String timestamp = (String) entry.get("timestamp");
String path = basePath + country + "-" + sensorID + "-" + timestamp + ".json";
DataStore store = new DataStore();
store.storeJSON(path, entry);
} }
} }

View File

@ -0,0 +1,43 @@
package sh.adb.sensorCommunityAPI;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class dataStore {
public boolean storeJson(String path, JSONObject json) throws IOException {
boolean check = false;
FileWriter file = new FileWriter(path);
try {
file.write(json.toJSONString());
check = true;
}catch (IOException e){
System.out.println("Can not write in file: " + path);
e.printStackTrace();
}
return check;
}
public JSONObject getjson(String path) throws IOException, ParseException {
JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
try {
Object obj = parser.parse(new FileReader(path));
jsonObject = (JSONObject) obj;
} catch (Exception e) {
System.out.println("Can not create Jsonobjekt from path: " + path);
e.printStackTrace();
}
return jsonObject;
}
}