|
|
|
package sh.adb.sensorCommunityAPI;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.json.simple.JSONArray;
|
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
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 APIParser {
|
|
|
|
String url;
|
|
|
|
public APIParser(String urlString) {
|
|
|
|
this.url = urlString;
|
|
|
|
}
|
|
|
|
public JSONArray getJSONObject() throws IOException, ParseException {
|
|
|
|
JSONParser parser = new JSONParser();
|
|
|
|
return (JSONArray) 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;
|
|
|
|
}
|
|
|
|
}
|