【问题标题】:Why is the API responding with response code 400?为什么 API 以响应代码 400 响应?
【发布时间】:2020-05-08 20:49:02
【问题描述】:

我正在尝试制作一个将文本和扬声器 ID 发送到 API 的应用程序,该 API 将文本转换为语音并将其发送回。

我的问题是 API 响应代码 400。URL 是正确的。请求方法正确。

(您可以查看 API 并找到更多信息 here

我还将在下面包含我的代码:

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import org.json.simple.JSONObject;
import java.io.IOException;

class Variables {
    String tekst;
    int speakerID = 7;

}

public class Neurokone {

    public void ConnectToAPIAndSendRequest() throws IOException {
        Variables v = new Variables();

        //Establishes a connection w/ the API.
        URL url = new URL("https://neurokone.cloud.ut.ee/api/v1.0/synthesize");
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type", "application/json; utf-8");
        con.setRequestProperty("Accept", "audio/wav");

        //This is self explanatory.
        JSONObject jb = new JSONObject();
            jb.put("text:", v.tekst);
            jb.put("speaker_id:", v.speakerID);

            //Sends the request.
            try(OutputStream os = con.getOutputStream()) {
                byte[] input = jb.toString().getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }


            try {
                //Generate a name for the response file based on user input
                String fileName = v.speakerID + "--" + v.tekst.substring(0,10);
                //Download response file
                ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
                FileOutputStream fos = new FileOutputStream(fileName);
                FileChannel fc = fos.getChannel();

                fc.transferFrom(rbc, 0, Long.MAX_VALUE);
            } catch (NullPointerException e) {
                System.out.println("Sinu koodis on viga: " + e);
            }
    }

    public static void main(String[] args) throws IOException {
        Variables variables = new Variables();
        Scanner skanner = new Scanner(System.in);

        //Here i am asking user to choose speaker voice and insert text to be sent to the API.
        System.out.print("Sisesta häälekood (7 - 10): ");
        variables.speakerID = skanner.nextInt();
        System.out.print("Sisesta sünteesitav tekst: ");
        variables.tekst = skanner.nextLine();
        variables.tekst = skanner.nextLine();
        skanner.close();

        new Neurokone().ConnectToAPIAndSendRequest();

    }


}

我还在学习,所以任何其他提示也非常欢迎

【问题讨论】:

  • 欢迎来到 SO。如果您需要这方面的帮助,您应该包括完整的错误详细信息,例如堆栈跟踪。代码 400 通常意味着您正在发送格式错误的请求。错误中可能暗示了哪一部分格式错误。
  • 哦,我的错。 Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://neurokone.cloud.ut.ee/api/v1.0/synthesize at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1927) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1523) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224) at Neurokone.ConnectToAPIAndSendRequest(Neurokone.java:48) at Neurokone.main(Neurokone.java:70)

标签: java httpurlconnection


【解决方案1】:

你有两个问题:

  jb.put("text:", v.tekst);
  jb.put("speaker_id:", v.speakerID);

text: -> textspeaker_id: -> speaker_id 没有 :

第二个问题是文档 speaker_id -> speakerId,但如果将其留空,则默认为 7

这是我的 curl 请求:

curl -X POST \
  https://neurokone.cloud.ut.ee/api/v1.0/synthesize \
  -d '{ "text": "Hello", "speakerId": "4"}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多