4

i want to use google speech api, i've found this https://github.com/gillesdemey/google-speech-v2/ where everything is explained well, but and im trying to rewrite it into java.

File filetosend = new File(path);
byte[] bytearray = Files.readAllBytes(filetosend);
URL url = new URL("https://www.google.com/speech-api/v2/recognize?output="+outputtype+"&lang="+lang+"&key="+key);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//method
conn.setRequestMethod("POST");
//header
conn.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");

now im lost... i guess i need to add the bytearray into the request. in the example its line

--data-binary @audio/good-morning-google.flac \

but httpurlconnection class has no method for attaching binary data.

hnnn
  • 504
  • 2
  • 7
  • 24

3 Answers3

4

But it has getOutputStream() to which you can write your data. You may also want to call setDoOutput(true).

Simon Fischer
  • 1,154
  • 6
  • 22
3

The code below works for me. I just used commons-io to simplify, but you can replace that:

    URL url = new URL("https://www.google.com/speech-api/v2/recognize?lang=en-US&output=json&key=" + key);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
    IOUtils.copy(new FileInputStream(flacAudioFile), conn.getOutputStream());
    String res = IOUtils.toString(conn.getInputStream());
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
0

Use multipart/form-data encoding for mixed POST content (binary and character data)

//set connection property
connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + <random-value>);

PrintWriter writer = null;
OutputStream output = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, charset), true);


// Send binary file.
writer.append("--" + boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append("\r\n");
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()).append("\r\n");
writer.append("Content-Transfer-Encoding: binary").append("\r\n");
writer.append("\r\n").flush();
Udit Saini
  • 101
  • 1
  • what exactly stands for boundary at 2nd and 10th line? – hnnn Sep 27 '14 at 20:12
  • 3
    Where did the OP or the respective API docs ask for multipart? – Simon Fischer Sep 27 '14 at 20:16
  • @hnnn Boundary is the hexadecimal (base-16) representation of the current time in milliseconds. As per the API it allows single pass processing of nested multipart streams. Visit http://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/MultipartStream.html – Udit Saini Sep 27 '14 at 20:32
  • and also im not sure if i can use multipart thing with google api – hnnn Sep 27 '14 at 20:41