Search code examples
androidandroid-asynctaskprogressdialogtelnet

progress dialog displays after task is finished in asynctask


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();
}
}

Solution

  • 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();
        }
    
    }