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) {}
}
I AM USING THIS CLASS LIKE BELOW.THERE IS BUTTON AND WHENEVER I CLICK THE BUTTON IT SHOULD START FTP UPLOAD PROCESS IN BACKGROUND BUT I GET “PROGRAM HAS STOPPED UNFORTUNATELY” ERROR.
Assume that The ftp address and username password pathfile sections are true and I get the internet and network permissions already by the way …
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
new Asenkron().execute("ftpaddress","username","pass","pathfileon telephone");
}
});
And here is the logcat for you to analyse the potential error and help me …
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
By the way I changed the relevant code like that ;
instead of
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
I replaced with this code
catch (Exception e)
{
HATA=e.toString();
}
And I added the code to button
textview1.setText(HATA);
So I can see the error on the textview and it is writing that
“Android java.net.UnknownHostException: Host is unresolved”
But i know that the ftp server is correct and I check the ftp server from the AndFTP application.
With the same address login and pass information ftp server is working.So the problem is in my code I think.Any help will be too much appreciated.Anyone who can help me I can give teamviewer to analyse what is the problem …
You can get a hint of the problem from this line:
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(). You’re trying to execute a UI operation from a thread that is not the main thread. All UI changes should be executed from the main thread.Comment out this line and it’ll work:
Instead of showing the toast to notify the user of the exception, use a
booleanto tell theActivitysubclass(that calls theasynctask) that there had been an exception and show a toast accordingly.EDIT- The Library you’re using might not be able to do the job, so you can just shift to a new library, like the apache commons ftp. Refer this blog to see how to use it.
EDIT2- If in
con.connect(aurl[0]);,aurl[0]already containshttp://***, you’ll have to removehttp://because the ftp client appends it later (causing the unknown host). Also change this linecon.setFileType(FTP.BINARY_FILE_TYPE);(remove thehttp://part)