Curretly, I can upload .wav files by using this command in Terminal (MAC):
curl -H 'X-Arg: AccessKey=3f55abc5-2830-027ce-e328-4e74df5a3f8f' --data-binary @audio.wav -H 'Content-Type: audio/wav' http://generic-15327.2-3-11.com/bbb.aaa.eval/abc
I want to do the same from Android. So far I have implemented an AsyncTask with the URLConnection inside, I send the headers and everything, except the wave file. Get a json response telling that no file was uploaded, obviously, but so far so good, I know I implemented the headers and connectivity correctly. Now, I want to upload the .wav file to wrap everything up, but I am a bit lost here. How can I upload a .wav file in Android, by using URLConnection ??? Below is my code, it is inside an AsyncTask which I will not put, to keep things short:
URL obj = new URL(BASE_URL);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("X-Arg", "AccessKey=3fcb4985-2830-07ce-e998-4e74df5a3f8f");
conn.setRequestProperty("Content-Type", "audio/wav");
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
String wavpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/audio.wav";
File wavfile = new File(wavpath);
boolean success = true;
if (wavfile.exists()) {
app.loge("**** audio.wav DETECTED");
}
else{
app.loge("**** audio.wav MISSING");
}
conn.connect();
//CODE TO UPLOAD THE FILE SHOULD GO HERE!!
int responseCode = conn.getResponseCode();
app.logy("POST Response Code : " + responseCode);
//Fetch response JSON
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
app.loge(response.toString());
result = 1;
} else {
app.loge("POST FAILED");
result = 0;
}
I finally found a way to do upload wav files. Some servers have slightly different ways of accepting or rejecting an upload request. In this other question URLConnection always returns 400 : Bad Request when I try to upload a .wav file I was trying to accomplish this same task on a different server, and proved to be more complicated than the one I am about to share in this answer, but take a look at it if you still cant make things work. There is no short way of directly "translating" a CURL request into java without messing with the NDK. For this question, this is the code that worked for me:
It's simpler code that wrote for wit.ai I hope it helps: