2014-01-22 105 views
-2

我想連接FTP並獲取文件列表。這是我的代碼:執行doInBackground時發生錯誤

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 
+0

你在lib文件夾中使用了一些外部的jar文件嗎? – nKn

回答

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

未找到阿帕奇FTPClient庫。

您必須將庫添加到您的項目中。重要的是將庫複製到您的項目「庫」文件夾。然後,你可以參考這種方式(假設你使用Eclipse):

項目 - >屬性 - > Java構建路徑 - >庫 - >添加罐

不要忘了剔庫在添加jar之後的「Order and Export」中。

+0

用戶似乎正在實施他自己的這種方法,所以我相信這不是一個外部庫(這就是爲什麼我問它) – nKn

+1

那麼,完整的類名是「org.apache.commons.net.ftp.FTPClient」所以我想它的外部:) – kupsef

+0

我在庫中添加jar,但我無法運行項目。 – user3223600

相關問題