Skip to main content

API Integration

The standard data interchange format of JSON simplifies the integration of REST APIs in most modern programming languages, including Java.

Using Java

Here’s how you can make calls to the Discover API using Java.

    URL url = new URL("https://signals.smarte.pro/signals/domains/subscribe");
String req = "{\n" +
" \"domain\": \"nividous.com\",\n" +
"}";
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("content-type", "application/json");
httpURLConnection.setRequestProperty("Api-Key", "mIGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxHJY=");
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(req.getBytes());
os.flush();
os.close();
int responseCode = httpURLConnection.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);

if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}

While Java does not include native JSON support out of the box, there are several robust libraries available to facilitate JSON processing. For instance, if you're using Java EE (now Jakarta EE), you can leverage the JSON Object class to construct JSON request payloads.

  JsonObject json =
Json.createObjectBuilder().add("domain", "nividous.com").build();

Similarly, the response could also be read as:

    StringReader reader = new StringReader(httpResponse.body());
JsonObject responseJson = null;
try (JsonReader jsonReader = Json.createReader(reader)) {
responseJson = jsonReader.readObject();
System.out.println(responseJson.getString("status"));
}