C# 通过Java将Speex/Flac文件发布到Google语音搜索API

C# 通过Java将Speex/Flac文件发布到Google语音搜索API,c#,java,http-post,voice-recognition,C#,Java,Http Post,Voice Recognition,我正在尝试构建一个Java应用程序,它连接到Google语音搜索API。 我开发了一个C#程序,运行良好 C#: 这里是Java代码,它不起作用: RandomAccessFile accessFile = new RandomAccessFile("temp.flac", "r"); byte[] bs = new byte[(int)accessFile.length()]; accessFile.read(bs); try { Ht

我正在尝试构建一个Java应用程序,它连接到Google语音搜索API。 我开发了一个C#程序,运行良好

C#:

这里是Java代码,它不起作用:

RandomAccessFile accessFile = new RandomAccessFile("temp.flac", "r");
byte[] bs = new byte[(int)accessFile.length()];
accessFile.read(bs);

        try
        {
            HttpURLConnection httpURLConnection = null;
            URL url = new URL("http", "www.google.com", "/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
            httpURLConnection = (HttpURLConnection)url.openConnection();

            int contentLength = bs.length;

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setInstanceFollowRedirects(false); 
            httpURLConnection.setUseCaches (false);

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");
            httpURLConnection.setRequestProperty("Content-Length", Integer.toString(contentLength));

            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.write(bs);
            dataOutputStream.flush();

            Scanner scanner = new Scanner(httpURLConnection.getInputStream()).useDelimiter("\\A");
            System.out.println(scanner.toString());

        }
        catch ( Exception e)
        {
           System.out.println(e.toString());
        }
我收到异常“java.io.IOException:写入服务器时出错”

有人知道我犯了什么错误吗? 提前谢谢你的帮助

Thx和问候
Linoge

构建libastral.so失败了。。。定义
不起作用
。打印出您遇到的任何异常或收到的任何错误。异常消息是“向服务器写入错误”,为什么不使用接受整个URL字符串的常规URL构造函数?好的,但我该如何做?我不熟悉URL构造函数。
newURL(“http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0“
RandomAccessFile accessFile = new RandomAccessFile("temp.flac", "r");
byte[] bs = new byte[(int)accessFile.length()];
accessFile.read(bs);

        try
        {
            HttpURLConnection httpURLConnection = null;
            URL url = new URL("http", "www.google.com", "/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
            httpURLConnection = (HttpURLConnection)url.openConnection();

            int contentLength = bs.length;

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setInstanceFollowRedirects(false); 
            httpURLConnection.setUseCaches (false);

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");
            httpURLConnection.setRequestProperty("Content-Length", Integer.toString(contentLength));

            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.write(bs);
            dataOutputStream.flush();

            Scanner scanner = new Scanner(httpURLConnection.getInputStream()).useDelimiter("\\A");
            System.out.println(scanner.toString());

        }
        catch ( Exception e)
        {
           System.out.println(e.toString());
        }