The Final and Working Code (I hope it helps you)
Finally, after hours, I managed to get it right! For those who need the answer, here it is:
public class Principal extends AppCompatActivity {
public static TextView texto;
String contents;
ProgressBar barra;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
texto = (TextView) findViewById(R.id.texto);
TarefaDownload download = new TarefaDownload();
download.execute();
}
The code above is my MainActivity (I call it "Principal" here). I created only a TextView there, then I instaciated my AsyncTask class called "TarefaDownload". This class is a private class where all the logic to acess ftp is placed. Let's see this class code now.
private class TarefaDownload extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("my_ftp_link_here", 21);
ftpClient.enterLocalPassiveMode();
ftpClient.login("my_user_login_here", "my_password_here");
ftpClient.changeWorkingDirectory("/");
InputStream inStream = ftpClient.retrieveFileStream("teste.txt");
InputStreamReader isr = new InputStreamReader(inStream, "UTF8");
int data = isr.read();
contents = "";
while(data != -1){
char theChar = (char) data;
contents = contents + theChar;
data = isr.read();
}
isr.close();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
texto.setText(contents);
}
}
So, basically I was trying to read a single line string from a txt file called "teste". The method "doInBackground" runs everything in background (don't you say?), so all the code to access ftp must come there.
Then I created a String called "contents", started to read the chars (one per time) from the InputStreamReader and storing in the string. You must notice that the String contents is being used in this method, but it belongs to my MainActivity, so I can access it outside the AsyncTask class. Finally, when de doInBackground method finishes, the "onPostExecute" methos is called and set the text of my TextView to value of my String value.
That's all! You may notice that you must add a INTERNET permission on your Manifest file (or then you'll not be able to access the ftp server):
<uses-permission android:name="android.permission.INTERNET" />
And that's it, your app should be reading data from the ftp server!