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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:40:27+00:00 2026-06-13T14:40:27+00:00

This question was asked and answered some time ago link . However the answer

  • 0

This question was asked and answered some time ago link. However the answer did not help me yet. I am in sort of the same position as the OP of the other post:
I have an Asynctask in which I make a connection to a certain website, however, most of the time the connection will take some time due to a laggy internetconnection.
I want the user to be able to stop trying to connect at any time.

public class DownloadWebHtml extends AsyncTask<String, Void, Map<String,ArrayList<ArrayList<String>>>> {        
        HttpURLConnection con = null;

        @Override
        protected void onPreExecute(){
            Button but = (Button) findViewById(301);
            but.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel(true);
                    con.disconnect();
                }
            });
        }

        @Override
        protected Map<String, ArrayList<ArrayList<String>>> doInBackground(String... urlcodes) {
            //stuff
            BufferedReader in = null;
            try {
                URL url = new URL("some url");
                con = (HttpURLConnection) url.openConnection();
                in = new BufferedReader(new InputStreamReader(con.getInputStream(),Charset.forName("ISO-8859-1")));
                String line = "";
                while ((line = in.readLine()) != null && !isCancelled()) {
                    html.add(line);
                }
                in.close();
            } catch (MalformedURLException e) {
                return null;
            } catch (IOException e) {
                return null;
            } finally {
                con.disconnect();
                 if (in != null) {
                     try {
                         in.close();
                     } catch (IOException e) {
                         Log.d("gettxt", e.getMessage());
                     }
                 }
            }

            if(!html.isEmpty()) {
                return //stuff;
            }
            return null;

        }

        @Override
        protected void onCancelled(){
            //cancellation
        }


    //onpostexecute doing stuff
}

Whenever the button is pressed the whole AsyncTask will by cancelled only after a connection has been made.
Is it possible to immediately stop the whole process on a button press?
Can it be done using the default httpurlconnection?

I tried using the disconnect to trigger an exception while the con.getInputStream is busy, but it failed to work.

  • 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-13T14:40:28+00:00Added an answer on June 13, 2026 at 2:40 pm

    I recommend switching to Apache’s HTTP Components. It works a charm, and has a thread-safe abort method, as outlined here.

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

Sidebar

Related Questions

This question is asked (and answered) just to share some knowledge with the SO
This question was asked and answered more than a year ago. Since then, ColdFusion
This question was previously asked Here , but not answered, And failed to find
So this question has been asked before , but not answered in great detail.
This question has been already asked, but never answered. I want to write some
This question is already asked but was still not answered. ( here ) I
This morning I asked this very similar question, and it was answered beautifully. However,
I feel like this question has already been asked and answered, yet I couldn't
This question has been asked a couple times but not answered in such a
This question was asked earlier by someone else but never answered: MKAnnotation - Map

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.