我使用 FTPClient 将文件(由用户选择)上传到我的 FTPServer。并且用于上传的代码正在工作。:) 但是当我为“上传...”对话框添加 AsyncTask 时,该对话框向用户显示它正在上传。它在我的应用程序中显示了上传对话框,然后它崩溃了,并且上传对话框没有消失....
来自 LogCat 的以下错误:
11-06 12:01:05.153: I/System.out(27121): ARGUMENT :: /storage/sdcard0/data-app/Z29sb2NrZXJpbmRlcGVuZGVudHZlcnNpb24
11-06 12:01:05.188: D/dalvikvm(27121): GC_CONCURRENT freed 107K, 9% free 12896K/14151K, paused 1ms+1ms, total 13ms
11-06 12:01:13.923: I/System.out(27121): status :: 211-Status of 'ProFTPD'
11-06 12:01:13.923: I/System.out(27121): Connected from 109.164.221.167 (109.164.221.167)
11-06 12:01:13.923: I/System.out(27121): Logged in as ftp031220
11-06 12:01:13.923: I/System.out(27121): TYPE: BINARY, STRUcture: File, Mode: Stream
11-06 12:01:13.923: I/System.out(27121): No data connection
11-06 12:01:13.923: I/System.out(27121): 211 End of status
11-06 12:01:25.988: W/System.err(27121): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-06 12:01:25.988: W/System.err(27121): at android.os.Handler.<init>(Handler.java:121)
11-06 12:01:25.988: W/System.err(27121): at android.app.Dialog.<init>(Dialog.java:107)
11-06 12:01:25.988: W/System.err(27121): at android.app.AlertDialog.<init>(AlertDialog.java:114)
11-06 12:01:25.993: W/System.err(27121): at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
11-06 12:01:25.993: W/System.err(27121): at android.app.AlertDialog$Builder.show(AlertDialog.java:931)
11-06 12:01:25.993: W/System.err(27121): at com.mseiz.give.your.apps.upload.upload(upload.java:124)
11-06 12:01:25.993: W/System.err(27121): at com.mseiz.give.your.apps.upload$DownloadFilesTask.doInBackground(upload.java:153)
11-06 12:01:25.993: W/System.err(27121): at com.mseiz.give.your.apps.upload$DownloadFilesTask.doInBackground(upload.java:1)
11-06 12:01:25.993: W/System.err(27121): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-06 12:01:25.993: W/System.err(27121): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-06 12:01:25.993: W/System.err(27121): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-06 12:01:25.993: W/System.err(27121): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-06 12:01:25.993: W/System.err(27121): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-06 12:01:25.993: W/System.err(27121): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-06 12:01:25.993: W/System.err(27121): at java.lang.Thread.run(Thread.java:856)
==== 点击项目... ====
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new DownloadFilesTask().execute(path.get(position), file.getName());
}
}
==== 异步任务 ====
private class DownloadFilesTask extends AsyncTask<String, Integer, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(upload.this);
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle("Datei wird hochgeladen...");
dialog.setMessage("Bitte warten...");
dialog.setIndeterminate(true);
dialog.show();
}
protected String doInBackground(String... args){
System.out.println("ARGUMENT :: " + args[0]);
upload(args[0], args[1]);
return root;
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
==== 上传功能 ====
public void upload(String upload, String datei)
{
try {
connectFTP("176.28.25.46");
// Prepare file to be uploaded to FTP Server
File file = new File(upload);
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
if(ftpClient.storeFile("/subdomains/giveyourapps/httpdocs/apps/"+datei, ifile)){
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Datei wurde hochgeladen!")
.setPositiveButton("OK", null).show();
} else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Fehler!")
.setPositiveButton("OK", null).show();
}
ftpClient.disconnect();
}catch (Exception e) {
e.printStackTrace();
}
}