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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:15:00+00:00 2026-05-20T20:15:00+00:00

In my application I download and parse a html page. However, I want to

  • 0

In my application I download and parse a html page. However, I want to be able to stop the download in its tracks (i.e. when the user hits cancel).

This is the code I use now, which is being called from doInBackground from ASyncTask.

How do I cancel this request from outside of the ASyncTask?

I currently use htmlcleaner

   HtmlCleaner cleaner = new HtmlCleaner();
   CleanerProperties props = cleaner.getProperties();
   props.setAllowHtmlInsideAttributes(true);
   props.setAllowMultiWordAttributes(true);
   props.setRecognizeUnicodeChars(true);
   props.setOmitComments(true);
   try {
        URL url = new URL(urlstring);
        URLConnection conn = url.openConnection();
        TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
        return node;
   } catch (Exception e) {
       failed = true;
       return;
   }
  • 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-05-20T20:15:00+00:00Added an answer on May 20, 2026 at 8:15 pm

    Ok, I believe I’ve solved this.

    In my Activity class I have a variable (boolean) failed. Also, I have a private Downloader class within the activity which extends ASyncTask. This way, the Downloader class has access to the failed boolean. When the Activity launches, it starts the Downloader task and a progress dialog pops up. When the task finishes, it closes the dialog and then goes on processing the downloaded content.

    However, when the user cancels the progress dialog, failed is set to true, and the user is sent back to the previous activity by a call to finished. In the meantime, Downloader is still busy downloading. Because the results are now unneccessary, we want it to stop using resources asap. In order to accomplish this, I have broken up the doInBackground method in as much steps as possible. After each step I check if failed is still false, when it is set to true, it simply doesn’t go to the next step. See it in action below. Furthemore, the BufferedReader reader is public, and in the onCancelled method I execute reader.close(). This will throw all sorts of exceptions, but these are properly caught.

     public void DoInBackground(.........) {
        try {
            URL url = new URL(uri);
            URLConnection conn = url.openConnection();
            if (!failed) {
                isr = new InputStreamReader(conn.getInputStream());
                if (!failed) {
                    reader = new BufferedReader(isr);
                    publishProgress(1);
                    if (!failed) {
                        TagNode node = cleaner.clean(reader);
                        publishProgress(2);
                        return node;
                    }
                }
            }
         } catch (Exception e) {
              failed = true;
              Log.v("error",""+e);
         } 
     }
    
    @Override
    protected void onCancelled() {
        failed = true;
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {
                failed = true;
            }
        if (isr != null)
            try {
                isr.close();
            } catch (IOException e) {
            }
    }
    

    I know that I could have broken up the downloading process in even tinier bits, but I am downloading very small files, so it’s not that important.

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

Sidebar

Related Questions

I want to download this open source application, and they are using Git. What
I want my web application users to download some data as an Excel file.
I have an application where user can download documents. User has option to download
I need in my application to download directories and their content. So I decided
I am writing a little application to download files over http (as, for example,
I have a problem regarding Android App. I have created an application that download
My application lets users download their data as CSV files. These are supposed to
We're writing a flash application that can download a MP3 file, convert it to
I'm supposed to use Amazon s3 in my application to upload/download files. Can anyone
The application I'm working on is using a background thread to download a list

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.