In case you had some issues in the previous steps, here is the full class code with a better displayed response.
Just remember to replace the <API_KEY>
token in the code by your API key (collected during step 2).
package demo;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.util.Scanner;
public class DemoChangePointDetection {
public static void main(String[] args) {
DataOutputStream dataOut = null;
BufferedReader in = null;
try {
String url = "https://sandbox.api.sap.com/ml/changepointdetection/inference_sync";
URL urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
// setting request method
connection.setRequestMethod("POST");
// adding headers
connection.setRequestProperty("content-type", "multipart/form-data; boundary=---011000010111000001101001");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("APIKey", "<API_KEY>");
connection.setDoInput(true);
// sending POST request
connection.setDoOutput(true);
// read the input file name from user input
Scanner scanner = new Scanner(System.in);
// Reading from System.in
String default_data = "93969.262,76604.444,50000.000,17364.818,-17364.818,-50000.000,-76604.444,-93969.262,-100000.000,-93969.262,-76604.444,-50000.000,-17364.818,17364.818,50000.000,76604.444,93969.262,100000.000";
System.out.println("What is your series data : (example : " + default_data + ")");
String data = scanner.nextLine();
if (data == null || data.length() == 0) {
data = default_data;
}
String default_separator = ",";
System.out.println("What is your value separator (default: \"" + default_separator + "\") : ");
String separator = scanner.nextLine();
if (separator == null || separator.length() > 1 || separator.length() == 0) {
separator = default_separator;
}
String default_series_separator = ":";
System.out.println("What is your series separator (default: \"" + default_series_separator + "\") : ");
String series_separator = scanner.nextLine();
if (series_separator == null || series_separator.length() > 1 || series_separator.length() == 0) {
series_separator = default_series_separator;
}
scanner.close();
// prepare the constant for the form data
String LINE_FEED = "\r\n";
String SEPARATOR = "--";
String BOUNDARY = "------Boundary" + new BigInteger(128, new SecureRandom()).toString(32);
// set the form content as multipart
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// write the form data content
dataOut = new DataOutputStream(connection.getOutputStream());
dataOut.writeBytes(SEPARATOR + BOUNDARY + LINE_FEED);
dataOut.writeBytes("Content-Disposition: form-data; name=\"options\"" + LINE_FEED);
dataOut.writeBytes(LINE_FEED);
dataOut.writeBytes(
"{ \"separator\" : \"" + separator + "\", \"series_separator\" : \"" + series_separator + "\" }");
dataOut.writeBytes(LINE_FEED);
dataOut.writeBytes(SEPARATOR + BOUNDARY + LINE_FEED);
dataOut.writeBytes("Content-Disposition: form-data; name=\"texts\"" + LINE_FEED);
dataOut.writeBytes(LINE_FEED);
dataOut.writeBytes(data);
// finish the form content
dataOut.writeBytes(LINE_FEED);
dataOut.writeBytes(SEPARATOR + BOUNDARY + SEPARATOR + LINE_FEED);
dataOut.flush();
dataOut.close();
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
} else {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// printing response
String TAB = "\t";
String QUOTE = "\"";
String CR = "\r\n";
System.out.println(response.toString()//
.replace(" " + QUOTE + "", "" + TAB + "" + QUOTE + "")//
.replace(" ", "" + TAB + "")//
.replace("" + TAB + " ", "" + TAB + "")//
.replace(", ", ",")//
.replace(": {", ":{" + CR + "")//
.replace(": [", ":[" + CR + "")//
.replace(":" + CR + "" + TAB + "{", ": {")//
.replace(":" + CR + "" + TAB + "[", ": [")///
.replace("{" + TAB + "", "{" + CR + "" + TAB + "")//
.replace("[" + TAB + "", "[" + CR + "" + TAB + "")//
.replace("" + QUOTE + ",", "" + QUOTE + "," + CR + "")//
.replace("," + TAB + "", "," + CR + "" + TAB + "")//
.replace("" + QUOTE + "" + TAB + "", "" + QUOTE + "" + CR + "" + TAB + "")//
.replace("" + TAB + " " + TAB + "", "" + TAB + "" + TAB + "")//
.replace("" + TAB + " {", "" + TAB + "{")//
.replace("" + TAB + " [", "" + TAB + "[")//
.replace("]", "]" + CR + "")//
.replace("}", "}" + CR + "")//
.replace("]" + CR + "," + CR + "", "]," + CR + "")//
.replace("}" + CR + "," + CR + "", "}," + CR + "")//
.replace("[" + CR + "]", "[]")//
.replace("{" + CR + "}", "{}")//
.replaceAll("([0-9])(\t)", "$1" + CR + "$2"));
} catch (Exception e) {
// do something with exception
e.printStackTrace();
} finally {
try {
if (dataOut != null) {
dataOut.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
// do something with exception
e.printStackTrace();
}
}
}
}