/* * Copyright 2012 Nexiwave Canada. All rights reserved. * Nexiwave Canada PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class SimpleNexiwaveSample { public static void main(String[] args) throws Exception { // Change these: String user = "myname@mycompany.com"; String passwd = "XYZ"; File wavFile = new File("/data/test8/sample.wav"); // Make the url: String url = "https://api.nexiwave.com/SpeechIndexing/file/storage/"+user+"/recording/?authData.passwd="+passwd+"&response=application/json&transcriptFormat=html&auto-redirect=true"; URL u = new URL(url); // Open the connection and prepare to POST HttpURLConnection uc = (HttpURLConnection)u.openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setAllowUserInteraction(false); uc.setConnectTimeout(20 * 60 * 1000); uc.setRequestProperty("content-type", "audio/vnd.wav"); uc.connect(); // Send the audio: OutputStream out = uc.getOutputStream(); int len; byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(wavFile); while((len = fis.read(buf)) >= 0){ out.write(buf, 0, len); } out.close(); fis.close(); // Read Response InputStream in = uc.getInputStream(); BufferedReader r = new BufferedReader(new InputStreamReader(in)); StringBuffer jsonStr = new StringBuffer(); String line; while ((line = r.readLine())!=null) { jsonStr.append(line); } in.close(); r.close(); uc.disconnect(); /** * Parse JSON response. You will need a json-lib package. Download here: http://sourceforge.net/projects/json-lib/files/json-lib/ * You will also need a few apache common packages. * * Or, use your own favorite json parser. */ JSONObject json = (JSONObject)JSONSerializer.toJSON(jsonStr.toString()); String transcript = json.getString("text"); // Perform the magic here: System.out.println(transcript); } }