Click here to Skip to main content
15,304,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating an android app too write and run Java code in an android device using Android studio we tried using the jdoodle API to compile and run the Java code in the app. But whenever we tried to compile the code ye are getting error 400. Please tell me how can I implement this API so that I can execute a Java file. we are using Java language to write the program

Getting error code 400 while executing this:

What I have tried:

Java
Thread thread = new Thread(new Runnable() {

u/Override

public void run() {

try {

try {

String clientId = "af52e5b3d******c2d81bade***6c055"; //Replace with your client ID

String clientSecret = "e868414f******183267d398d578a448c6a1a70bf8be2896ebc******b013bc"; //Replace with your client Secret

String script = editText.getText().toString();

String language = "java";

String versionIndex = "0";

URL url = new URL("https://api.jdoodle.com/v1/execute");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoOutput(true);

connection.setRequestMethod("POST");

connection.setRequestProperty("Content-Type", "application/json");

String input = "{\"clientId\": \"" + clientId + "\",\"clientSecret\":\"" + clientSecret + "\",\"script\":\"" + script +

"\",\"language\":\"" + language + "\",\"versionIndex\":\"" + versionIndex + "\"} ";

System.out.println(input);

OutputStream outputStream = connection.getOutputStream();

outputStream.write(input.getBytes());

outputStream.flush();

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

throw new RuntimeException("Please check your inputs : HTTP error code : " + connection.getResponseCode());

}

BufferedReader bufferedReader;

bufferedReader = new BufferedReader(new InputStreamReader(

(connection.getInputStream())));

StringBuilder results = new StringBuilder();

String output1;

System.out.println("Output from JDoodle .... \n");

while ((output1 = bufferedReader.readLine()) != null) {

results.append(output1);

}

connection.disconnect();

output.setText(results);

} catch (IOException e) {

e.printStackTrace();

}

} catch (Exception e) {

e.printStackTrace();

}

}

});

thread.start();
Posted
Updated 3-Dec-21 5:02am
v2

1 solution

See the examples at Compiler API FAQ - Documentation[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900