AsyncTask和Telnet没有显示ProgressDialog

时间:2014-06-04 14:07:52

标签: android android-asynctask telnet progressdialog

我正在进行一些telnet操作时使用asynctask。但是,没有显示progressdialog,我几乎100%确定Telnet是原因。

请查看我的代码并帮助我找出问题所在。

由于

public class TelnetManager extends AsyncTask<String, Void, String>{

private TelnetClient telnet;
private int port;
private String IP;
private ProgressDialog dialog;
private Context context;

public TelnetManager(Context c,String IP, int port, String user, String pass)
{
    context=c;
    this.IP=IP;
    this.port=port;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog=new ProgressDialog(context);
    dialog.setMessage(context.getResources().getString(R.string.msg_wait));
    dialog.show();
}

public String readString() throws IOException
{
    InputStream in = new BufferedInputStream(telnet.getInputStream());

int read=0;

    String s=null;
     do
        {
           byte[] buffer = new byte[1024];
            read = in.read(buffer);
            if(read > 0)
            {
                if(s==null)s=new String(buffer, 0, read);
                else s+=new String(buffer, 0, read);
                Log.e("S",s);
            }
        }
        while (read > 0);

     in.close();
     return s;
}

public void writeString(String command) throws IOException
{
    OutputStream out = telnet.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");
    writer.write(command+'\n');
    writer.flush();
}

@Override
protected String doInBackground(String... params) {
    telnet = new TelnetClient();
    String s="";
    try {
        telnet.setConnectTimeout(10000);
        telnet.connect(IP,port);
        telnet.setKeepAlive(true);

        writeString("password");
        writeString(params[0]);
        writeString("exit");
        String aux=readString();
        telnet.getInputStream().close();

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    try {
        telnet.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return s;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    if(dialog!=null && dialog.isShowing())
    {
        dialog.dismiss();
    }
}

}

这就是我称之为AsyncTask的地方:

String list=null;

try {

    list=new TelnetManager(this,"192.168.11.30", 10010, null, null).execute("son").get();
    construirLayout(list,R.id.containerON);
    list=new TelnetManager(this,"192.168.11.30", 10010, null, null).execute("soff").get();
    construirLayout(list,R.id.containerOFF);

} 
catch (InterruptedException e) {
    Toast.makeText(this,"InterruptedException",3000).show();
    e.printStackTrace();
} 
catch (ExecutionException e) {
    Toast.makeText(this,"ExecutionException",3000).show();
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:1)

如果您call get() on an AsyncTask,则告诉UI线程阻止并等待AsyncTask结果。由于UI线程被阻止,因此无法显示ProgressDialog。

您应该提供对AsyncTask的回调,在onPostExecute()中触发。