Android file upload with ICS
<p>Till Android 3.0 I can upload a file to my server using the following code:
With ICS, the code return filenotfoundexception when using httpurlconnection.getinputstream
Someone suggest to convert this code using HTTPPOST, but honest I don't know how to do.</p>

<p>Some suggestion?</p>

<pre><code>public class FileUpload extends AsyncTask&lt;Void, Integer, Void&gt; {

@Override
protected void onProgressUpdate(Integer... progress) {
    dialog.setProgress(progress[0]);
}

private ProgressDialog dialog;
private String reponse_data;
private String file_path;
private JobInstance localJob;
private Context localContext;
private CharSequence testo;
private HttpURLConnection connection;

// can use UI thread here
protected void onPreExecute() {
    dialog = new ProgressDialog(localContext);
    dialog.setMessage("Uploading...");
    dialog.setIndeterminate(false);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);
    dialog.show();
}

@Override
protected Void doInBackground(Void... arg0) {

    try {
        File file = new File(file_path);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        fileInputStream.read(bytes);
        fileInputStream.close();

        URL url = new URL("https://myserver.com/functionUpload?lang=it-IT");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
        connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));

        if(Build.VERSION.SDK_INT &lt; 11){
            connection.setDoOutput(true);
        }



        //connection.connect();     
        OutputStream outputStream = connection.getOutputStream();

        int bufferLength = 1024;
        for (int i = 0; i &lt; bytes.length; i += bufferLength) {
            int progress = (int) ((i / (float) bytes.length) * 100);
            publishProgress(progress);
            if (bytes.length - i &gt;= bufferLength) {
                outputStream.write(bytes, i, bufferLength);
            } else {
                outputStream.write(bytes, i, bytes.length - i);
            }
            outputStream.flush();
        }

        publishProgress(100);

        outputStream.close();


    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        int risp = connection.getResponseCode();
            DataInputStream inStream = new DataInputStream ( connection.getInputStream() );
        String str;
        while (( str = inStream.readLine()) != null){
            Log.e("Debug","Server Response "+str);
            reponse_data=str;

                JSONObject jObject = new JSONObject(reponse_data);
                JSONArray menuObject = jObject.getJSONArray("hypotheses");
                testo = menuObject.getJSONObject(0).getString("utterance").toString();
        }
        inStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;




}

@Override
protected void onPostExecute(Void result) {
    if (this.dialog.isShowing()) {
        this.dialog.dismiss();
    }
    Toast.makeText(localContext, testo, Toast.LENGTH_LONG).show();
}

public void executeBefore(Context context, JobInstance j) {
    localContext = context;
    dialog = new ProgressDialog(localContext);
    localJob = j;
    this.file_path = localJob.getFinalfileposition().getAbsolutePath();
    this.execute();

}
</code></pre>

<p>}</p>
