You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中集成JDoodle API实现Java代码编译运行并解决400报错

JDoodle API调用返回400错误解决方案

核心问题原因

你当前代码的核心问题是手动拼接JSON请求体,未处理用户输入Java代码中的特殊字符(双引号、换行符、转义符等),导致提交的JSON格式非法,服务端无法解析直接返回400错误。此外你还存在参数类型错误的问题:versionIndex 字段JDoodle要求为整型,你当前作为字符串类型传入,也会触发参数校验失败。

具体修复步骤

  • 停止手动拼接JSON,使用Android原生的JSONObject构造请求体,自动处理转义逻辑
  • 修正versionIndex的参数类型,传入整型而非字符串
  • 提前校验用户输入的Java代码是否包含完整的可运行结构(必须包含main方法)
  • 确认JDoodle后台申请的clientId、clientSecret有效,且当日调用配额未耗尽

修正后的代码示例

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        String clientId = "af52e5b3d******c2d81bade***6c055"; //替换为你自己的client ID
        String clientSecret = "e868414f******183267d398d578a448c6a1a70bf8be2896ebc******b013bc"; //替换为你自己的client Secret
        String script = editText.getText().toString();
        String language = "java";
        int versionIndex = 0; //修正为整型

        HttpURLConnection connection = null;
        try {
            URL url = new URL("https://api.jdoodle.com/v1/execute");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");

            //用JSONObject构造请求体,自动处理转义
            JSONObject requestJson = new JSONObject();
            requestJson.put("clientId", clientId);
            requestJson.put("clientSecret", clientSecret);
            requestJson.put("script", script);
            requestJson.put("language", language);
            requestJson.put("versionIndex", versionIndex);
            String input = requestJson.toString();

            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(input.getBytes());
            outputStream.flush();

            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                //读取错误响应方便排查问题
                BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                StringBuilder errorMsg = new StringBuilder();
                String line;
                while ((line = errorReader.readLine()) != null) {
                    errorMsg.append(line);
                }
                errorReader.close();
                throw new RuntimeException("请求失败,错误码:" + responseCode + ",错误信息:" + errorMsg);
            }

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder results = new StringBuilder();
            String output1;
            while ((output1 = bufferedReader.readLine()) != null) {
                results.append(output1);
            }
            bufferedReader.close();
            //更新UI要切回主线程
            runOnUiThread(() -> output.setText(results.toString()));

        } catch (Exception e) {
            e.printStackTrace();
            runOnUiThread(() -> output.setText("执行失败:" + e.getMessage()));
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
});
thread.start();

额外注意事项

如果修改后仍然报错,可打印请求体和服务端返回的错误详情,JDoodle的错误响应会明确说明是参数错误、配额不足还是代码格式问题,根据错误信息调整即可。

内容的提问来源于stack exchange,提问作者Saurabh Bhoir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 12:54:05