class Asenkron extends AsyncTask<String,Integer,Long>
{
@Override
protected Long doInBackground(String... aurl)
{
FTPClient con=null;
try
{
con = new FTPClient();
con.connect(aurl[0]);
if (con.login(aurl[1], aurl[2]))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(http://FTP.BINARY_FILE_TYPE);
FileInputStream in = new FileInputStream(new File(aurl[3]));
boolean result = con.storeFile(aurl[3], in);
in.close();
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
protected void onPostExecute(String result) {}
}
我正在使用这个类,如下所示。有按钮,当我单击按钮时,它应该在后台启动 FTP 上传过程,但我得到“程序已不幸停止”错误。 假设 ftp 地址和用户名密码路径文件部分为真,并且我已经获得了互联网和网络权限...
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
new Asenkron().execute("ftpaddress","username","pass","pathfileon telephone");
}
});
这是logcat,供您分析潜在错误并帮助我...
10-13 13:01:25.591: I/dalvikvm(633): threadid=3: reacting to signal 3
10-13 13:01:25.711: I/dalvikvm(633): Wrote stack traces to '/data/anr/traces.txt'
10-13 13:01:25.921: D/gralloc_goldfish(633): Emulator without GPU emulation detected.
10-13 13:01:31.441: W/dalvikvm(633): threadid=11: thread exiting with uncaught exception (group=0x409c01f8)
10-13 13:01:31.461: E/AndroidRuntime(633): FATAL EXCEPTION: AsyncTask #1
10-13 13:01:31.461: E/AndroidRuntime(633): java.lang.RuntimeException: An error occured while executing doInBackground()
10-13 13:01:31.461: E/AndroidRuntime(633): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-13 13:01:31.461: E/AndroidRuntime(633): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.lang.Thread.run(Thread.java:856)
10-13 13:01:31.461: E/AndroidRuntime(633): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-13 13:01:31.461: E/AndroidRuntime(633): at android.os.Handler.<init>(Handler.java:121)
10-13 13:01:31.461: E/AndroidRuntime(633): at android.widget.Toast$TN.<init>(Toast.java:317)
10-13 13:01:31.461: E/AndroidRuntime(633): at android.widget.Toast.<init>(Toast.java:91)
10-13 13:01:31.461: E/AndroidRuntime(633): at android.widget.Toast.makeText(Toast.java:233)
10-13 13:01:31.461: E/AndroidRuntime(633): at com.example.ftpodak.ODAK$Asenkron.doInBackground(ODAK.java:74)
10-13 13:01:31.461: E/AndroidRuntime(633): at com.example.ftpodak.ODAK$Asenkron.doInBackground(ODAK.java:1)
10-13 13:01:31.461: E/AndroidRuntime(633): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-13 13:01:31.461: E/AndroidRuntime(633): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-13 13:01:31.461: E/AndroidRuntime(633): ... 5 more
顺便说一句,我像这样更改了相关代码;
代替
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
我用这段代码替换
catch (Exception e)
{
HATA=e.toString();
}
我将代码添加到按钮
textview1.setText(HATA);
所以我可以在 textview 上看到错误,它写的是“Android java.net.UnknownHostException: Host is unresolved”
但我知道 ftp 服务器是正确的,我从 AndFTP 应用程序检查了 ftp 服务器。使用相同的地址登录并传递信息 ftp 服务器正在工作。所以我认为问题出在我的代码中。任何帮助将不胜感激。任何可以帮助我的人我都可以让 teamviewer 分析问题是什么......