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 8591423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:31:31+00:00 2026-06-11T23:31:31+00:00

I am using a AsyncTask to download a file from a URL in Android.

  • 0

I am using a AsyncTask to download a file from a URL in Android.
This is the class to download the file in the background:

//-----------------------------ASYNC DOWNLOADER--------------------------------
    /**
     * Background Async Task to download file
     * */
    class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @SuppressWarnings("deprecation")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        //TODO Zustzinfos für alle Methoden hinzufügen
        /**
         * This method is called for executing the background task in the AsyncTask.
         * For this tutorial we are only sleeping the thread for the number of 
         * seconds passed as parameter of the function.
         * 
         * @param numSeconds: life of the task
         * @return the result of the background task
         */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();
                // getting file length
                int lenghtOfFile = conection.getContentLength();     
                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(), 8192);     
                // Output stream to write file
                OutputStream output = new FileOutputStream("/sdcard/"+Name+".xml");

                byte data[] = new byte[1024];    
                long total = 0;  
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress(""+(int)((total*100)/lenghtOfFile));

                    // writing data to file
                    output.write(data, 0, count);
                }
                // flushing output
                output.flush();  
                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("ASYNC",e.getMessage());
            }
            return null;
        }    
        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
       }     
        /**
        * After completing background task
        * Dismiss the progress dialog
        * **/
        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(String error) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);
            String xmlPath = Environment.getExternalStorageDirectory().toString() + "/"+Name+".xml";
            Log.d("XMLDOWNLOADPATH", xmlPath);
            Log.d("DOWNLOADXML","End Download XML file");
        }

    }

I want to be able to recognize in my main activity (the async class is a inner class of the main acticity) if there was a exception and show the error message in a Dialog or Toast.

I tried to return the value from doInBackground() to onPostExecute() and write this String to a global String variable like this:

protected String doInBackground(String... f_url) {
String error = null;
try {
}
catch(Exception e){
error = e.toString();
}
return error;

@Override
protected void onPostExecute(String error) {
globalStringvariable = error;
}

But this does not work properly, the dialog does not always show the exception message. Is this the best way to realize my problem?

  • 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-11T23:31:32+00:00Added an answer on June 11, 2026 at 11:31 pm
    e.getMessage();
    

    That’s what you’re looking for.
    Also note that if your getting null, it can be because you are catching Exception as a generic, you should always try to catch a specific Exception in order to get the message back.

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

Sidebar

Related Questions

I am using this code (inside an AsyncTask) to download files: URL u =
I'm using this tutorial on AsyncTask with a task and a notification: https://eliasbland.wordpress.com/2011/03/11/an-example-of-how-to-run-a-background-task-and-report-progress-in-the-status-bar-using-asynctask-on-android/ What
I have a async task called within an activity: public class DownloadFile extends AsyncTask<String,
i have a code which download the apk from server by using Asynctask, Currently
i am using asynctask to fetch images from given url.these images are displaying in
I have to download a file and I'm using this code , which is
I am getting images from background process by using url.To dispaly images i have
I have a code that downloads videos from a URL. I am using AsyncTask.
I'm using AsyncTask to download and parse XML content from i-net. Downloading and parsing
I'a using a asynctask to download file. It works normally until i turn off

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.