2

嗨,我想显示progressdialog直到通过telnet. 所以我asynctask用于这个目的。

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

}

asynctaskoncreate()下面这样打电话

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

    try{   

         new AsyncAction().execute();

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

}

progressdialog问题是在命令执行之前我看不到。请帮我解决问题。提前致谢。

编辑

发送和读取命令的代码

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

3 回答 3

1

目前您正在尝试从 onPostExecute 执行网络操作,因为此方法是从 UI Thread 调用的。更改您的代码以便以正确的方式工作

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

}
于 2013-01-21T19:36:52.357 回答
0

您需要在 onPreExecute() 中显示进度条在 doInBackground() 中完成工作,然后在 onPostExecute() 中隐藏进度条。onPreExecute() 和 onPostExecute() 都在主线程上执行,而 doInBackground 在后台执行。

于 2013-01-21T19:34:27.743 回答
0

您正在onPostExecute()方法上进行工作,该方法应该在doInBackground()内 显示您的进度对话框onPreExecute()并在onPostExecute()内关闭。

于 2020-06-13T12:10:25.873 回答