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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…