import java.net.URL;
import java.net.HttpURLConnection;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import org.json.JSONObject;
public class Leonardo {
public static void main(String[] args) {
DataOutputStream dataOut = null;
BufferedReader in = null;
try {
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
System.out.print("? ");
String text = br.readLine();
String url = "https://sandbox.api.sap.com/ml/languagedetection/language";
URL urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
//setting request method
connection.setRequestMethod("POST");
//adding headers
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Accept","application/json");
connection.setRequestProperty("APIKey","/*yourAPIKey*/");
connection.setDoInput(true);
//sending POST request
connection.setDoOutput(true);
dataOut = new DataOutputStream(connection.getOutputStream());
dataOut.writeBytes("{ \"message\": \"" + text + "\"}");
dataOut.flush();
int responseCode = connection.getResponseCode();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
//printing response
JSONObject obj = new JSONObject(response.toString());
String langCode = obj.getString("langCode");
if (langCode.equals("de")) {
System.out.println("Willkommen zu SAP Leonardo maschinelles Lernen!");
} else {
System.out.println("Welcome zu SAP Leonardo Machine Learning!");
}
} 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();
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.