我想連接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
你在lib文件夾中使用了一些外部的jar文件嗎? – nKn