【发布时间】:2017-01-13 15:20:58
【问题描述】:
我正在尝试使用 UrlConnection 发送音频(表示为 base64 字节数组 - 根据 Google Cloud Speech API 的要求)。我正在使用 ubuntu 的 Raspberry Pi 上执行此操作。如果我在笔记本电脑上使用它(也与 ubuntu 一起使用),连接确实有效。我正在使用带有 URLConnection 的 java。崩溃发生在 OutputStream out = urlConnection.getOutputStream();
Java 代码:
// URL to google + parameter with the API key.
URL googleUrl = new URL(GOOGLE_RECOGNIZER_URL + "?key=" + APIKEY);
// Open New URL connection channel.
URLConnection urlConnection;
urlConnection = googleUrl.openConnection();
// we want to do output. (input is automaticly set on true).
urlConnection.setDoOutput(true);
// No caching.
urlConnection.setUseCaches(false);
// Add headers.
urlConnection.setRequestProperty("Content-Type", "application/json");
// For sending the recorded data to google it needs to be first
// converted to a byte array.
byte[] array1 = AudioToByteArray.readAudioFileData(file);
// And next convert the byte array to a base64 string.
String audioString = Base64.getEncoder().encodeToString(array1);
// Make (JSON) Strings for configuration and location of audio file.
String testConfig = "\"config\": { \"encoding\":\"LINEAR16\", \"sampleRate\": 16000, \"languageCode\": \"nl-NL\"}";
String testAudioLoc = "\"audio\": { \"content\": \"" + audioString + "\" }";
String together = "{ " + testConfig + "," + testAudioLoc + " }";
// Send the string to google.
OutputStream out = urlConnection.getOutputStream();
OutputStreamWriter wr = new OutputStreamWriter(out);
wr.write(together);
wr.flush();
wr.close();
out.close();
错误:
javax.net.ssl.SSLException: java.lang.IllegalStateException
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1906)
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1889)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1410)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1316)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at main.ContactGoogleCloudSpeechAPI.writeOut(ContactGoogleCloudSpeechAPI.java:98)
at main.ContactGoogleCloudSpeechAPI.contactGoogleUrlCon(ContactGoogleCloudSpeechAPI.java:41)
at main.SpeechRecognition.contactAPI(SpeechRecognition.java:184)
at main.SpeechRecognition.mainLoop(SpeechRecognition.java:104)
at main.SpeechRecognition.run(SpeechRecognition.java:81)
at main.SpeechRecognition.main(SpeechRecognition.java:62)
【问题讨论】:
-
stackoverflow.com/questions/37167189/… 为我解决了这个问题
标签: java ubuntu outputstream urlconnection