为什么API的响应代码是400?

问题描述 投票:0回答:1

我试图制作一个应用程序,将文本和说话者ID发送到API,将文本转换为语音并发送回来。

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

(你可以看看API,找到更多的信息) 此处)

我也会在下面附上我的代码。

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();

    }


}

我还在学习,所以也非常欢迎任何其他的技巧。

java httpurlconnection
1个回答
2
投票

你有两个问题。

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

text: -> text >和 speaker_id: -> speaker_id 没有 :

第二个问题是文件的问题speaker_id -> speakerId但如果你把它留空,它默认为7。

这是我的卷发要求。

curl -X POST \
  https://neurokone.cloud.ut.ee/api/v1.0/synthesize \
  -d '{ "text": "Hello", "speakerId": "4"}'
© www.soinside.com 2019 - 2024. All rights reserved.