Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:13:02+00:00 2026-06-07T03:13:02+00:00

Till Android 3.0 I can upload a file to my server using the following

  • 0

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.

Some suggestion?

public class FileUpload extends AsyncTask<Void, Integer, Void> {

    @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 < 11){
                connection.setDoOutput(true);
            }



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

            int bufferLength = 1024;
            for (int i = 0; i < bytes.length; i += bufferLength) {
                int progress = (int) ((i / (float) bytes.length) * 100);
                publishProgress(progress);
                if (bytes.length - i >= 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();

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:13:05+00:00Added an answer on June 7, 2026 at 3:13 am

    My Content-Lenght” should be “Content-Length”, I fix also the code in the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a progress bar in android, so that till my data is
I have been using this code to parse xml till now. Its was working
I need a little help with sending an HttpUrlConnection from my android application. Till
till now i was using drawable to populate marker on my map .Now i
How can i wait till webbrowser loaded the page? i tried: webbrowser1.navigate(url) msgbox(done)
How to stop thread till other thread is running. How can i check thread
Can any one help with how to test sensor events like shake on Android
I am targeting my application to be able run across Android 1.5 till 2.3.
I'm relatively new to Android but I just cant google this. I have following
I have prepared a code to just play a simple mp4 file from my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.