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

  • Home
  • SEARCH
  • 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 9158775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:19:46+00:00 2026-06-17T13:19:46+00:00

Hi i want to display progressdialog until a command is executed through telnet .

  • 0

Hi i want to display progressdialog until a command is executed through telnet.
so i use asynctask for that purpose.

private class AsyncAction extends AsyncTask<String, Void, String> 
{

    @Override
    protected String doInBackground(String... arg0) 
    {
        // TODO Auto-generated method stub


        return null;
    }

    @Override
    protected void onPostExecute(String result) 
    {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        try
          {

              telnet.connect("XXX.XXX.XXX.XXX", 23);

                // Get input and output stream references
                in = telnet.getInputStream();
                out = new PrintStream(telnet.getOutputStream());

                // Log the user on
                readUntil("login:");
                write("jk");
                readUntil("password:");
                write("kk");

                // Advance to a prompt
                readUntil(prompt + "");
                write("ping -t localhost\n");
                readUntil(">");
                write("cdkk");


                AlertDialog.Builder alertbox = new AlertDialog.Builder(TelSampActivity.this);
                String msg="work finished!";
                alertbox.setMessage(msg);
                alertbox.show();

        } 
          catch (Exception e) 
          {
            // TODO: handle exception
        }
        finally
        {
            pd.dismiss();
        }





//          pd.dismiss();
    }

    @Override
    protected void onPreExecute() 
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(TelSampActivity.this);
        pd.setMessage("loading...");
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();
    }

}

And i call asynctask in oncreate() like below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{   

         new AsyncAction().execute();

        }catch (Exception e) {
            e.printStackTrace();
        }

}

The problem is that i could not see progressdialog until command executes.
please help me solve the issue.
Thanks in advance.

EDIT

The code to send and read command

public String readUntil(String pattern) {
    try {
    char lastChar = pattern.charAt(pattern.length()-1);
    StringBuffer sb = new StringBuffer();
    boolean found = false;
    char ch = (char) in.read();
    while (true) {
    System.out.print(ch);
    sb.append(ch);
if (ch == lastChar) 
{
if (sb.toString().endsWith(pattern)) 
{
    if (sb.toString().contains("cdkk"))
    {
        disconnect();
        break;


    }
    else
    {
        return sb.toString();
    }

}
else
{
    disconnect();
    break;
}
}
else if(sb.toString().contains("Failed"))
{
    AlertDialog.Builder alertbox = new AlertDialog.Builder(TelSampActivity.this);
        String error="Invalid username or password!";
        alertbox.setMessage(error);
        alertbox.setTitle("Error");
        alertbox.show();
    System.out.println("bad user name");
    disconnect();
    break;
}

ch = (char) in.read();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

public void write(String value) {
try {
out.println(value);
out.flush();
System.out.println(value);
}
catch (Exception e) {
e.printStackTrace();
}
}

public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + " ");
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

public void disconnect() {
try {
telnet.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}
  • 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-17T13:19:47+00:00Added an answer on June 17, 2026 at 1:19 pm

    Currently you are trying to do Network operations from onPostExecute because this method called from UI Thread . change your code as to get work in proper way

    private class AsyncAction extends AsyncTask<String, Void, String> 
    {
         public static boolean status=false;
        @Override
        protected String doInBackground(String... arg0) 
        {
            // TODO Auto-generated method stub
            try
              {
    
                  telnet.connect("XXX.XXX.XXX.XXX", 23);
    
                    // Get input and output stream references
                    in = telnet.getInputStream();
                    out = new PrintStream(telnet.getOutputStream());
    
                    // Log the user on
                    readUntil("login:");
                    write("jk");
                    readUntil("password:");
                    write("kk");
    
                    // Advance to a prompt
                    readUntil(prompt + "");
                    write("ping -t localhost\n");
                    readUntil(">");
                    write("cdkk");
                   // make status true or false if command successfully executed 
                  status=true;
    
            } 
              catch (Exception e) 
              {
                // TODO: handle exception
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) 
        {
    
           pd.dismiss();
             // check status if true then show AlertDialog
          if(status==true){
           AlertDialog.Builder alertbox = 
                            new AlertDialog.Builder(TelSampActivity.this);
           String msg="work finished!";
           alertbox.setMessage(msg);
           alertbox.show();
         }
        else{
               // your code here
           }
    
        }
    
        @Override
        protected void onPreExecute() 
        {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = new ProgressDialog(TelSampActivity.this);
            pd.setMessage("loading...");
            pd.setIndeterminate(true);
            pd.setCancelable(false);
            pd.show();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have AsyncTask extend class I want to display progress dialoge so I use
hey i want to display a loading progress using ProgressDialog on my aplication, but
I want to display a progressdialog when a client sends request to the server..the
I want display data from database in Listbox...Here is my code, It is not
i want display 1 record from colums zodys , I'm programint in C# I
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
In the header of the django admin, I want display a link. This link
I want to display images in a TableView . I can display one image
I want to display result of this javascript in a label control on 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.