【问题标题】:Google speech to text api v1: javax.net.ssl.SSLException: Broken pipeGoogle 语音转文本 api v1:javax.net.ssl.SSLException:断管
【发布时间】:2015-11-10 10:39:38
【问题描述】:

我正在使用谷歌语音到文本来记录通话并将其发送到谷歌的服务器以将 flac 音频文件转换为文本。在 android 5.0 和以前的版本中一切正常,但从 5.02 及更高版本开始出现以下错误

javax.net.ssl.SSLException: Write error: ssl=0x8f548: I/O error during system call, Broken pipe

我正在使用 google Speech api v1。谁能告诉我为什么会得到这个?

这是抛出异常的方法..

private Scanner openHttpsPostConnection(String urlStr, byte[] data) {
    InputStream in = null;
    byte[] mextrad = data;
    int resCode = -1;
    OutputStream out = null;
    // int http_status;
    try {
        URL url = new URL(urlStr);
        URLConnection urlConn = url.openConnection();

        if (!(urlConn instanceof HttpsURLConnection)) {
            throw new IOException("URL is not an Https URL");
        }

        HttpsURLConnection httpConn = (HttpsURLConnection) urlConn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);

        httpConn.setRequestMethod("POST");
        //httpConn.setReadTimeout(15*1000);
        httpConn.setDoOutput(true);
        httpConn.setChunkedStreamingMode(0);
        httpConn.setRequestProperty("Content-Type", "audio/x-flac; rate=1600");
                //+ sampleRate);
        httpConn.setRequestProperty("AcceptEncoding", "gzip,deflate,sdch");
        httpConn.connect();

        try {
            // this opens a connection, then sends POST & headers.
            out = httpConn.getOutputStream();
            // Note : if the audio is more than 15 seconds
            // dont write it to UrlConnInputStream all in one block as this
            // sample does.
            // Rather, segment the byteArray and on intermittently, sleeping
            // thread
            // supply bytes to the urlConn Stream at a rate that approaches
            // the bitrate ( =30K per sec. in this instance ).
            Log.d("ParseStarter", "IO beg on data");
            out.write(mextrad); // one big block supplied instantly to the
                                // underlying chunker wont work for duration
                                // > 15 s.
            Log.d("ParseStarter", "IO fin on data");
            // do you need the trailer?
            // NOW you can look at the status.
            resCode = httpConn.getResponseCode();

            Log.d("ParseStarter", "POST OK resp: " +resCode+" "
                    + httpConn.getResponseMessage().getBytes().toString());

            if (resCode / 100 != 2) {
                Log.d("ParseStarter", "POST bad io ");
            }

        } catch (IOException e) {
            Log.d("ParseStarter", "FATAL " + e);

        }

        if (resCode == HttpsURLConnection.HTTP_OK) {
            Log.d("ParseStarter", "OK RESP to POST return scanner ");
            return new Scanner(httpConn.getInputStream());
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

【问题讨论】:

  • 我们能看到给出错误的代码吗?
  • 我已经添加了代码请看一下

标签: java android ssl


【解决方案1】:

OutputStream 无法在服务器上写入,因为它是自己的。获取 OutputStream 后,您需要创建一个 BufferedWriter 以在服务器上写入。

.
.
.
try {
// this opens a connection, then sends POST & headers.
out = httpConn.getOutputStream();
// Note : if the audio is more than 15 seconds
// dont write it to UrlConnInputStream all in one block as this
// sample does.
// Rather, segment the byteArray and on intermittently, sleeping
// thread
// supply bytes to the urlConn Stream at a rate that approaches
// the bitrate ( =30K per sec. in this instance ).
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(mextrad);
.
.
.

编辑:抱歉,我忘了检查您的数据类型。那样的话,能不能换个试试:

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(mextrad);

DataOutputStream stream = new DataOutputStream(out);
stream.writeBytes(mextrad);

我之前从未在 Android 中尝试过,但我希望它会有所帮助。

【讨论】:

  • BufferedWriter 无法写入字节数组
  • 它仍然无法正常工作。您可能没有得到我的问题,我的应用程序在 android 5.0(lolipop) 中完美运行,但问题在该版本之后开始。
猜你喜欢
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多