I just managed to upload some files to an ftp server using apache ftp. when I use everything as it should be it works fine, I mean, the file uploading is working, but when I use, for example, a wrong password for the ftp account, obviously the files don't get uploaded to the server, but I don't get an error message either. in other words: I don't know how to show the error. I tried to show with a toast the e.getMessage() from the Exception but nothing is shown. Is there any documentation that would help me? or am I missing something you could help me with? thank you!
This is my code:
private class eXport extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
btn.setEnabled(false);
btnD.setEnabled(false);
btnS.setEnabled(false);
btnD.setBackgroundColor(getResources().getColor(R.color.gr));
btnS.setBackgroundColor(getResources().getColor(R.color.gr));
pbSum=100/adapter.getSelected().size();
pbPorc=0;
pb1.setProgress(5);
pb2.setProgress(5);
blurry.setVisibility(View.VISIBLE);
pb1.setVisibility(View.VISIBLE);
pb2.setVisibility(View.VISIBLE);
bloq=true;
}
@Override
protected Void doInBackground(Void... voids) {
FTPClient ftpClient=new FTPClient();
try {
ftpClient.connect(fserver);
ftpClient.login(fusername,fpassword);
ftpClient.enterLocalPassiveMode();
dirArch = new ArrayList<>();
for (int k=0;k<adapter.getSelected().size();k ){
dirArch.add(adapter.getSelected().get(k).getArch());
}
for (String archTXT : dirArch){
File fileX =new File(stringFolder,archTXT);
InputStream inputStream=new FileInputStream(fileX);
ftpClient.storeFile(archTXT,inputStream);
inputStream.close();
pbPorc=pbPorc pbSum;
pb1.setProgress(pbPorc);
pb2.setProgress(pbPorc);
}
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(ExportTracks.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid){
super.onPostExecute(aVoid);
btn.setEnabled(true);
btnD.setEnabled(true);
btnS.setEnabled(true);
btnD.setBackgroundColor(getResources().getColor(R.color.ro));
btnS.setBackgroundColor(getResources().getColor(R.color.gr));
blurry.setVisibility(View.INVISIBLE);
pb1.setVisibility(View.INVISIBLE);
pb2.setVisibility(View.INVISIBLE);
bloq=false;
//Toast.makeText(ExportTracks.this,getResources().getString(R.string.dexp),Toast.LENGTH_SHORT).show();
}
CodePudding user response:
Your error handling is wrong.
The FTPClient.login
does not throw an exception, when the credentials are wrong. It returns a false
. You do not test for that. Your code carries on, trying to work with not authorized session instead, all of which will obviously fail too.
But as the same is true for FTPClient.storeFile
, which also just returns false
, your code basically do not notice any errors.