执行doInBackground时发生错误

时间:2014-01-22 13:21:45

标签: android ftp android-asynctask

我想用FTP连接并从他们的文件列表中获取。这是我的代码:

public class MainActivity extends Activity {

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


    task getson = new task();
    getson.execute();

}

FTPClient client;

public FTPClient connectWithFTP() throws IOException{
     client = new FTPClient();  
     try {

         client.connect("server", 21);  
         client.login("username", "password");
         System.out.println("status :: " + client.getReplyString());
         int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new Exception("Connect failed: " + client.getReplyString());
            }
            try {
                client.enterLocalPassiveMode();
                if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
                    Log.v(getClass().toString(), "Setting binary file type failed.");
                }
            } catch(Exception e) {
                e.printStackTrace();
            } 
         checkFiles(client);
    } catch (Exception e) {
        System.out.println("status :: " + client.getReplyString());
    }
      return client;
}

private class task extends AsyncTask <Void, Void, FTPClient> {

    @Override
    protected void onPreExecute() {
        // Do stuff before the operation
    }


    @Override
    protected FTPClient doInBackground(Void... params) {
        FTPClient ftp = null;
        try {
            ftp = connectWithFTP();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

public void disconnectWithFTP() throws IOException{
    client.logout();  
    client.disconnect();  
}

private void checkFiles(FTPClient clients){
      try {  
          FTPFile[] ftpFiles = clients.listFiles();  
          int length = ftpFiles.length;  
          for (int i = 0; i < length; i++) {  
            String name = ftpFiles[i].getName();  
            Calendar date = ftpFiles[i].getTimestamp();
            Log.v("aasd", name+", "+date);

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

logcat的

01-22 20:05:46.994: E/AndroidRuntime(616): FATAL EXCEPTION: AsyncTask
#1 01-22 20:05:46.994: E/AndroidRuntime(616): java.lang.RuntimeException: An error occured while executing doInBackground() 01-22 20:05:46.994: E/AndroidRuntime(616):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-22 20:05:46.994:   E/AndroidRuntime(616):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)                    01-22 20:05:46.994: E/AndroidRuntime(616):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at java.lang.Thread.run(Thread.java:1019)   
01-22 20:05:46.994: E/AndroidRuntime(616): Caused by: java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient 
01-22 20:05:46.994: E/AndroidRuntime(616):  at com.example.listfile.MainActivity.connectWithFTP(MainActivity.java:38) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at com.example.listfile.MainActivity$task.doInBackground(MainActivity.java:75) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at com.example.listfile.MainActivity$task.doInBackground(MainActivity.java:1) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
01-22 20:05:46.994: E/AndroidRuntime(616):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
01-22 20:05:46.994: E/AndroidRuntime(616):  ... 4 more

1 个答案:

答案 0 :(得分:2)

Caused by: java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient
at com.example.listfile.MainActivity.connectWithFTP(MainActivity.java:38) 

找不到apache FTPClient库。

您必须将库添加到项目中。重要的是将库复制到项目“libs”文件夹中。然后你可以这样引用它(假设你使用Eclipse):

项目 - &gt;属性 - &gt; Java构建路径 - &gt;图书馆 - &gt;添加罐子

添加jar后,不要忘记在“订购和导出”中勾选库。