2017-04-13 70 views
-2

我已经为Android编写了一个网络(JSch)异步任务。出于某种原因,AsyncTask永远不会结束,直到我再次调用buttonClick()。这里是我的代码:Android - AsyncTask永无止境

public class MainActivity extends AppCompatActivity 

{ @覆盖 保护无效的onCreate(捆绑savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

protected void buttonClick(View view) 
    { 
      System.out.println(" --------------------------------- "); 
      System.out.println(" CONNECT BUTTON CLICKED!"); 
      System.out.println(" --------------------------------- "); 
      new AsyncTask<Void, Void, Void>() { 
        @Override 
        protected Void doInBackground(Void... params) 
        { 
          connect(); 
          System.out.println("Connect done"); 
          return null; 
        } 
        protected Void onPostExecute() 
        { 
          System.out.println("PostExec done"); 
          return null; 
        } 
      }.execute(); 
      System.out.println("Done with ASyncTask"); 
      String text = output; 
      TextView tv = (TextView)findViewById(R.id.ayy); 
      tv.setText(text); 
    } 

    private int d; 
    private String output; 
    private void debug() 
    { 
      System.out.println("Debug" + d); 
      d++; 
    } 

    private void connect() 
    { 
      d = 1; 
      debug(); 
      JSch jsch = new JSch(); 
      String host = "[edited out]"; 
      String user = "[edited out]"; 
      String password = "[edited out for obvious reasons]"; 
      String command = "uname -a"; 
      int port = 2223; 
      try { 
        debug(); 
        Session session = jsch.getSession(user, host, port); 
        debug(); 
        UserInfo ui = new MyUserInfo(); 
        debug(); 
        session.setUserInfo(ui); 
        debug(); 
        session.connect(); 
        debug(); 
        Channel channel = session.openChannel("exec"); 
        debug(); 
        ((ChannelExec) channel).setCommand(command); 
        debug(); 
        channel.setInputStream(null); 
        debug(); 
        ((ChannelExec) channel).setErrStream(System.err); 
        debug(); 
        InputStream in = channel.getInputStream(); 
        debug(); 
        channel.connect(); 
        debug(); 
        byte[] tmp = new byte[1024]; 
        while(true) { 
          while(in.available() > 0) { 
            int i = in.read(tmp, 0, 1024); 
            if(i < 0) break; 
            output = output + new String(tmp, 0, i); 
          } 
          if(channel.isClosed()) { 
            if(in.available() > 0) continue; 
            System.out.println("exit-status: " + channel.getExitStatus()); 
            break; 
          } 
          try { 
            Thread.sleep(1000); 
          } catch(Exception ee) { 

          } 
        } 
        channel.disconnect(); 
        session.disconnect(); 
      } catch(Exception e) { 
        System.err.println("Exception: " + e); 
        e.printStackTrace(); 
      } 
    } 
    } 

    class MyUserInfo implements UserInfo, UIKeyboardInteractive 
    { 
    public String getPassword() 
    { 
      return passwd; 
    } 

    public boolean promptYesNo(String str) 
    { 
        /*Object[] options = {"yes", "no"}; 
        int foo = JOptionPane.showOptionDialog(null, 
          str, 
          "Warning", 
          JOptionPane.DEFAULT_OPTION, 
          JOptionPane.WARNING_MESSAGE, 
          null, options, options[0]); 
        return foo == 0; */ 
      return true; 
    } 

    String passwd; 

    public String getPassphrase() 
    { 
      return null; 
    } 

    public boolean promptPassphrase(String message) 
    { 
      return true; 
    } 

    public boolean promptPassword(String message) 
    { 
      passwd = "[edited out]"; 
      return true; 
    } 

    public void showMessage(String message) 
    { 
      System.out.println("MSG: " + message); 
    } 

    /* 
    final GridBagConstraints gbc = 
      new GridBagConstraints(0, 0, 1, 1, 1, 1, 
        GridBagConstraints.NORTHWEST, 
        GridBagConstraints.NONE, 
        new Insets(0, 0, 0, 0), 0, 0); 
    private Container panel; 
    */ 
    public String[] promptKeyboardInteractive(String destination, 
               String name, 
               String instruction, 
               String[] prompt, 
               boolean[] echo) 
    { System.out.println("promptKeyboardInteractive");/* 
      panel = new JPanel(); 
      panel.setLayout(new GridBagLayout()); 

      gbc.weightx = 1.0; 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      gbc.gridx = 0; 
      panel.add(new JLabel(instruction), gbc); 
      gbc.gridy++; 

      gbc.gridwidth = GridBagConstraints.RELATIVE; 

      JTextField[] texts = new JTextField[prompt.length]; 
      for(int i = 0; i < prompt.length; i++) { 
        gbc.fill = GridBagConstraints.NONE; 
        gbc.gridx = 0; 
        gbc.weightx = 1; 
        panel.add(new JLabel(prompt[i]), gbc); 

        gbc.gridx = 1; 
        gbc.fill = GridBagConstraints.HORIZONTAL; 
        gbc.weighty = 1; 
        if(echo[i]) { 
          texts[i] = new JTextField(20); 
        } else { 
          texts[i] = new JPasswordField(20); 
        } 
        panel.add(texts[i], gbc); 
        gbc.gridy++; 
      } 

      if(JOptionPane.showConfirmDialog(null, panel, 
        destination + ": " + name, 
        JOptionPane.OK_CANCEL_OPTION, 
        JOptionPane.QUESTION_MESSAGE) 
        == JOptionPane.OK_OPTION) { 
        String[] response = new String[prompt.length]; 
        for(int i = 0; i < prompt.length; i++) { 
          response[i] = texts[i].getText(); 
        } 
        return response; 
      } else { 
        return null; // cancel 
      } 
    */ 
      return null; 
     } 
} 

我完全知道该代码是一塌糊涂,但是我将它清理干净,一旦我得到这个工作。

输出后1个按钮点击:按钮点击

输出后2
04-13 15:45:33.451 26826-26826/com.example.ryan.jschtest I/System.out: --------------------------------- 
04-13 15:45:33.451 26826-26826/com.example.ryan.jschtest I/System.out: CONNECT BUTTON CLICKED! 
04-13 15:45:33.451 26826-26826/com.example.ryan.jschtest I/System.out: --------------------------------- 
04-13 15:45:33.452 26826-26826/com.example.ryan.jschtest I/System.out: Done with Asynctask 
04-13 15:45:33.452 26826-26925/com.example.ryan.jschtest I/System.out: Debug1 
04-13 15:45:33.457 26826-26925/com.example.ryan.jschtest I/System.out: Debug2 
04-13 15:45:33.470 26826-26925/com.example.ryan.jschtest I/System.out: Debug3 
04-13 15:45:33.470 26826-26925/com.example.ryan.jschtest I/System.out: Debug4 
04-13 15:45:33.470 26826-26925/com.example.ryan.jschtest I/System.out: Debug5 
04-13 15:45:33.472 26826-26925/com.example.ryan.jschtest I/System.out: Connecting: 192.168.1.18:2223 
04-13 15:45:34.008 26826-26925/com.example.ryan.jschtest I/System.out: Debug6 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug7 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug8 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug9 
    04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug10 
    04-13 15:45:34.014 26826-26925/com.example.ryan.jschtest I/System.out: Debug11 
04-13 15:45:34.076 26826-26925/com.example.ryan.jschtest I/System.out: Debug12 
04-13 15:45:35.112 26826-26925/com.example.ryan.jschtest I/System.out: exit-status: 0 
04-13 15:45:35.115 26826-26925/com.example.ryan.jschtest I/System.out:  Connect done 

04-13 15:45:33.451 26826-26826/com.example.ryan.jschtest I/System.out: --------------------------------- 
04-13 15:45:33.451 26826-26826/com.example.ryan.jschtest I/System.out: CONNECT BUTTON CLICKED! 
04-13 15:45:33.451 26826-26826/com.example.ryan.jschtest I/System.out: --------------------------------- 
04-13 15:45:33.452 26826-26826/com.example.ryan.jschtest I/System.out: Done with Asynctask 
04-13 15:45:33.452 26826-26925/com.example.ryan.jschtest I/System.out: Debug1 
04-13 15:45:33.457 26826-26925/com.example.ryan.jschtest I/System.out: Debug2 
04-13 15:45:33.470 26826-26925/com.example.ryan.jschtest I/System.out: Debug3 
04-13 15:45:33.470 26826-26925/com.example.ryan.jschtest I/System.out: Debug4 
04-13 15:45:33.470 26826-26925/com.example.ryan.jschtest I/System.out: Debug5 
04-13 15:45:34.008 26826-26925/com.example.ryan.jschtest I/System.out: Debug6 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug7 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug8 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug9 
04-13 15:45:34.013 26826-26925/com.example.ryan.jschtest I/System.out: Debug10 
04-13 15:45:34.014 26826-26925/com.example.ryan.jschtest I/System.out: Debug11 
04-13 15:45:34.076 26826-26925/com.example.ryan.jschtest I/System.out: Debug12 
04-13 15:45:35.112 26826-26925/com.example.ryan.jschtest I/System.out: exit-status: 0 
04-13 15:45:35.115 26826-26925/com.example.ryan.jschtest I/System.out: Connect done 
04-13 15:47:32.784 26826-26826/com.example.ryan.jschtest I/System.out: --------------------------------- 
04-13 15:47:32.784 26826-26826/com.example.ryan.jschtest I/System.out: CONNECT BUTTON CLICKED! 
04-13 15:47:32.784 26826-26826/com.example.ryan.jschtest I/System.out: --------------------------------- 
04-13 15:47:32.785 26826-26826/com.example.ryan.jschtest I/System.out: Done with Asynctask 
04-13 15:47:32.785 26826-28694/com.example.ryan.jschtest I/System.out: Debug1 
04-13 15:47:32.786 26826-28694/com.example.ryan.jschtest I/System.out: Debug2 
04-13 15:47:32.786 26826-28694/com.example.ryan.jschtest I/System.out: Debug3 
04-13 15:47:32.786 26826-28694/com.example.ryan.jschtest I/System.out: Debug4 
04-13 15:47:32.786 26826-28694/com.example.ryan.jschtest I/System.out: Debug5 
04-13 15:47:33.370 26826-28694/com.example.ryan.jschtest I/System.out: Debug6 
04-13 15:47:33.370 26826-28694/com.example.ryan.jschtest I/System.out: Debug7 
04-13 15:47:33.370 26826-28694/com.example.ryan.jschtest I/System.out: Debug8 
04-13 15:47:33.370 26826-28694/com.example.ryan.jschtest I/System.out: Debug9 
04-13 15:47:33.370 26826-28694/com.example.ryan.jschtest I/System.out: Debug10 
04-13 15:47:33.371 26826-28694/com.example.ryan.jschtest I/System.out: Debug11 
04-13 15:47:33.436 26826-28694/com.example.ryan.jschtest I/System.out: Debug12 
04-13 15:47:34.467 26826-28694/com.example.ryan.jschtest I/System.out: exit-status: 0 
04-13 15:47:34.468 26826-28694/com.example.ryan.jschtest I/System.out: Connect done 
+0

您的代码无法读取 –

+0

@IvanMilisavljevic此代码非常糟糕,但肯定没那么糟糕? – Lightn1ng

回答

-1

解决:

public boolean connectComplete = false; 

protected void buttonClick(View view) 
{ 
    ConnectTask ayy = new ConnectTask(); 
    ayy.execute(); 
    while(!connectComplete) { 
     try { Thread.sleep(100); } catch(Exception e) {System.out.println(e); e.printStackTrace; } 
    } 
    ayy.cancel(true); 
    connectComplete = false; 
} 
在ConnectTask

@Override 
protected Void doInBackground(Void... params) 
{ 
     MainActivity.connect(); 
     cancel(true); 
     System.out.println("Connect done"); 
     connectComplete = true; 
     return null; 
} 
+0

这看起来像一个不好的解决方案。您正在主线程中休眠,这会阻塞应用程序的整个UI,并且正在等待异步任务完成,从而导致其异步运行的整个目的失败。 – RobCo

+0

@RobCo任务从未花费超过半秒钟,并在应用程序启动时运行。它使用网络,所以我被迫运行是异步的。有更好的解决方案来使用吗? – Lightn1ng

+0

更好的办法是将回调传递给它可以在完成时调用的任务,或者只需要在MainActivity中调用一个单独的方法。这应该在'onPostExecute'中完成,以便在主线程上再次调用它。这样主线程可以在doInBackground执行时做其他事情 – RobCo