I have a txt file
in my ftp server
. And all I want to do is reading that file, retrieve the contents and save it as a string by using AsyncTask
. As I understand from the logcat, I'm able to connect ftp server
and I can change directory. But I cannot read the context of the text file, and I don't know how to return it from the my AsyncTask
class.
And these are my codes:
MainActivity.java
package com.example.ftpdenemeleri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FtpAsync task1 = new FtpAsync();
task1.execute();
}
}
FtpAsync.java
package com.example.ftpdenemeleri;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPCmd;
import android.os.AsyncTask;
public class FtpAsync extends AsyncTask <Void, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("hostname", 21);
System.out.println(ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.USER, "user");
System.out.println(ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.PASS, "pass");
System.out.println(ftpClient.getReplyString());
ftpClient.sendCommand(FTPCmd.CWD, "/home/www/bitirme");
InputStream is = new BufferedInputStream(ftpClient.retrieveFileStream("beaglesays.txt"));
System.out.println("Input Stream has opened.");
} catch (IOException ex) {
System.err.println(ex);
}
return null;
}
protected void onPostExecute() {
// TODO: check this.exception
// TODO: do something with the feed
}
}