From 41455472c7c68ddfe8d495dda0d451c10d678aaf Mon Sep 17 00:00:00 2001 From: adb Date: Wed, 3 Mar 2021 09:42:18 +0100 Subject: [PATCH] add class APIPaser --- build.gradle | 1 + .../sh/adb/sensorCommunityAPI/APIPaser.java | 40 +++++++++++++++++++ .../java/sh/adb/sensorCommunityAPI/Main.java | 9 ++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java diff --git a/build.gradle b/build.gradle index cecfa2f..0ff86fd 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ repositories { dependencies { compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' + compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13' testCompile group: 'junit', name: 'junit', version: '4.12' } diff --git a/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java b/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java new file mode 100644 index 0000000..a40cd4f --- /dev/null +++ b/src/main/java/sh/adb/sensorCommunityAPI/APIPaser.java @@ -0,0 +1,40 @@ +package sh.adb.sensorCommunityAPI; + +import org.apache.http.client.methods.HttpGet; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import java.io.IOException; +import java.net.MalformedURLException; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.json.simple.parser.ParseException; + +public class APIPaser { + String url; + public APIPaser(String urlString) throws MalformedURLException { + this.url = urlString; + } + public Object getJSON() throws IOException, ParseException { + JSONParser parser = new JSONParser(); + return parser.parse(this.getRequest()); + } + public String getRequest() throws IOException { + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + HttpGet request = new HttpGet(this.url); + request.addHeader("content-type", "application/json"); + HttpResponse res = httpClient.execute(request); + String data = EntityUtils.toString(res.getEntity(), "UTF-8"); + System.out.println("API response => "); + System.out.println(data); + return data; + } catch (IOException ex) { + System.out.print("error while http Request"); + } + return null; + } +} diff --git a/src/main/java/sh/adb/sensorCommunityAPI/Main.java b/src/main/java/sh/adb/sensorCommunityAPI/Main.java index ff44a70..84ed97a 100644 --- a/src/main/java/sh/adb/sensorCommunityAPI/Main.java +++ b/src/main/java/sh/adb/sensorCommunityAPI/Main.java @@ -1,7 +1,12 @@ package sh.adb.sensorCommunityAPI; +import org.json.simple.parser.ParseException; + +import java.io.IOException; + public class Main { - public static void main(String[] args){ - System.out.print("hello world"); + public static void main(String[] args) throws IOException, ParseException { + APIPaser api = new APIPaser("https://data.sensor.community/airrohr/v1/sensor/35943/"); + api.getJSON(); } }