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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:15:45+00:00 2026-06-15T17:15:45+00:00

I have a problem which I don’t understand. I want to show a simple

  • 0

I have a problem which I don’t understand. I want to show a simple Progress Dialog in Android. So I created an AsyncTask and create the dialog in the constructor. I use the methods onPreExceution to initialise the dialog and the onPostExecute method I destory the dialog. So until now this looks total correct for me. But when I start the App on my Nexus 7 the dialog doesn’t show up till the job is done. So it shows up for a half of a second at the end of the job… What am I doing wrong?

Thank you for your help 😉

public class ParseHTMLCodeNew extends AsyncTask<String, Void, String> {
    ProgressDialog dialog;

    public ParseHTMLCodeNew(Context context) {
        dialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        //einrichten des Wartedialogs
        dialog.setTitle("Bitte warten!");
        dialog.setMessage("Die Kommentare werden vom Server geladen.");
        dialog.show();
    }

    @Override
    protected String doInBackground(String params) {
        InputStream is = null;
        String data = "";
        try
        {
            URL url = new URL( params[0] ); 
            is = url.openStream(); 
            data = new Scanner(is).useDelimiter("//html//").next(); 
        } 
        catch ( Exception e ) { 
            e.printStackTrace(); 
        } 
        return data;
    }

    @Override
    protected void onPostExecute(String result) {       
        //Dialog beenden RSS Feed ist fertig  geparst
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }        
    }
}

UPDATE

This is my new AsyncTask:

public class ParseHTMLCodeNew extends AsyncTask<String, String, String> {
ProgressDialog dialog;
private final OnCompleteTaskListener onCompleteTaskListener;

public interface OnCompleteTaskListener {
        void onComplete(String data);
}

public ParseHTMLCodeNew(Context context, OnCompleteTaskListener taskListener) {
    onCompleteTaskListener = taskListener;
    dialog = new ProgressDialog(context);
}


@Override
protected void onPreExecute() {

    //einrichten des Wartedialogs
    dialog.setTitle("Bitte warten!");
    dialog.setMessage("Die Kommentare werden vom Server geladen.");
    dialog.show();

}

@Override
protected String doInBackground(String... params) {
    InputStream is = null; 
    String data = "";
    try 
    { 
      URL url = new URL( params[0] ); 
      is = url.openStream(); 
      data = new Scanner(is).useDelimiter("//html//").next(); 
    } 
    catch ( Exception e ) { 
      e.printStackTrace(); 
    } 
    return data;
}

@Override
protected void onPostExecute(String result){

  onCompleteTaskListener.onComplete(result);

  //Dialog beenden RSS Feed ist fertig  geparst
  if (dialog != null && dialog.isShowing()) {
      dialog.dismiss();
  }




}
}

And i am calling it this way:

new ParseHTMLCodeNew(this,new OnCompleteTaskListener() {

        @Override
        public void onComplete(String data) {
            gData = data;

        }
    }).execute(url);

As i commented on your post, data has no value.

  • 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-15T17:15:47+00:00Added an answer on June 15, 2026 at 5:15 pm

    If you calling this code so:

    String data = new ParseHTMLCodeNew(CommentActivity.this).execute(url).get();
    

    Then you do not really see your dialogue because there is a blocking UI.
    Method get() waits if necessary for the computation to complete, and then retrieves its result.

    Call so:

    new ParseHTMLCodeNew(CommentActivity.this).execute(url);
    

    and the result of the work is handled directly in the AsyncTask.

    If you need to transfer the data to the main thread, you should tell him that the task was completed.
    Wat is the simple code, I just added OnCompleteTaskListener interface

    public class ParseHTMLCodeNew extends AsyncTask<String, Void, String> {
    
        private final OnCompleteTaskListener onCompleteTaskListener;
        private ProgressDialog dialog;
    
        public interface OnCompleteTaskListener {
            void onComplete(String data);
        }
    
        public ParseHTMLCodeNew(Context context, OnCompleteTaskListener taskListener) {
            onCompleteTaskListener = taskListener;
            dialog = new ProgressDialog(context);
        }
    
        @Override
        protected void onPreExecute() {
    
            // einrichten des Wartedialogs
            dialog.setTitle("Bitte warten!");
            dialog.setMessage("Die Kommentare werden vom Server geladen.");
            dialog.show();
    
        }
    
        @Override
        protected String doInBackground(String... params) {
            StringBuilder sb = new StringBuilder();
    
            // your code here
            try {
                for (int i = 0; i < 100; i++) {
                    Thread.sleep(100);
                    sb.append(i);
                }
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            return sb.toString();
        }
    
        @Override
        protected void onPostExecute(String result) {
    
            // Dialog beenden RSS Feed ist fertig geparst
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
    
            onCompleteTaskListener.onComplete(result);
        }
    }
    

    And the example of a call

    new ParseHTMLCodeNew(this,new OnCompleteTaskListener() {
    
                @Override
                public void onComplete(String data) {
                    Toast.makeText(CommentActivity.this, data, Toast.LENGTH_LONG).show();
    
                }
            }).execute("your_url");
    

    Be careful, this code can produce errors when you rotate your Phone.
    When Activity destroyed but task is performed:
    – progress dialog will close and will not open again
    – local variable to dialog or context is incorrect.

    If the operation is performed for a long time can make it through the of the services?

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

Sidebar

Related Questions

I am having a strange problem which I don't understand. I have the following
I have a problem which I don't understand and there doesn't seem to be
I have a problem which I don't know how to solve. I have created
I have a problem which I don't really know how to solve. I have
I have a problem which I don't know how to fix. It has to
I have a very specific problem which don't seem to be asked before -
So I have this problem, which I don't see how it's happening. Basically I
I have this weird problem which I don't remember to ever had in XCode
I'm new to PHP, and I have stumble on the problem which I don't
I have a problem with nested routes which don't display correct url. I use

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.