Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old February 8th, 2012, 05:28 AM   #1 (permalink)
Junior Member
 
Valten1992's Avatar
 
Join Date: Feb 2012
Location: Ireland
Posts: 34
 
Device(s): Samsung Galaxy mini
Thanks: 5
Thanked 3 Times in 3 Posts
Default FTP download problems

Huzzah! My first problem post!(not sure if that is a good thing or a bad thing.....)

A problem that has been frustrating me for the last few days is downloading from an FTP for my Android app. Up till now, the app has been reading from local files to get its data but the final version shall read the files on a FTP server. The problem is that while the experiment app I made can connect to the server, downloading seems to not work. No file is downloaded at all, knowing me Im probably missing something obvious.

My onCreate
Code:
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        
         ftpConnect("domain","username", "password", portnum);
         
        
         try
         {
             String s = Environment.getExternalStorageDirectory().toString();
             File f = new File(s+"/???/item");
             f.mkdirs();
             ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
         }
         catch(Exception e)
         {
             Toast toast = Toast.makeText(getApplicationContext(), "Download error: "+e.getMessage(), Toast.LENGTH_SHORT);
             toast.show();
         }
        
         ftpDisconnect();
        
        
    }
Names hidden to protect identity


And the download method

Code:
public boolean ftpDownload(String srcFilePath, File desFilePath)
    {
        boolean status = false;
        try {
            
            FileOutputStream desFileStream = new FileOutputStream(desFilePath);
            
            status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
            desFileStream.close();
            Toast toast = Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT);
            toast.show();

            return status;
        } catch (Exception e) {
            Toast toast = Toast.makeText(getApplicationContext(), "Download error"+e.getMessage(), Toast.LENGTH_SHORT);
            toast.show();
        }
        

        return status;
    }
Unrelated, but how exactly do you make your own custom logs to display errors? I've been using toasts up until now. I try Log.e("Error", e.getMessage()) but it does not display.

Valten1992 is offline  
Reply With Quote
Sponsors
Old February 8th, 2012, 09:54 AM   #2 (permalink)
Moderati ergo sum
 
lunatic59's Avatar
 
Join Date: Jun 2010
Location: Pennsylvania
Posts: 13,483
 
Device(s): Galaxy S II, Captivate, Nexus 1 (retired), Nook Color, Asus Transformer.
Thanks: 2,611
Thanked 5,150 Times in 3,022 Posts
Default

This is probably more suited to our development forums. This area is used to discuss applications in general from a user standpoint. I can move the thread if you'd like.
__________________
If the information is useful, Like the post, if the person was helpful, Thank the member.
_
RULES: Lest we forget | This part of signature intentionally left blank.
lunatic59 is online now  
Reply With Quote
Old February 8th, 2012, 10:05 AM   #3 (permalink)
Junior Member
 
Valten1992's Avatar
 
Join Date: Feb 2012
Location: Ireland
Posts: 34
 
Device(s): Samsung Galaxy mini
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Oh thank you, please do.
Valten1992 is offline  
Reply With Quote
Old February 8th, 2012, 01:34 PM   #4 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

In FTP, the initial connection to port 21 is only used for sending commands and receiving status replies in reaction to those commands. Data is transferred on a separate FTP data connection

In the default FTP active mode, it's the FTP server establishes the FTP data connection back with the client. If the FTP client is behind a NAT device or a firewall, the server is not going to be able to connect. The symptom of this problem is as you describe, the FTP exchange works until it comes time to transfer data.

In FTP passive mode, the FTP client establishes the FTP data connection. To enter passive mode, you need to send a PASV command to the server just before you send the RETR command. The reply to the PASV command will include the port number on the server to which the client needs to open the FTP data connection. See RFC 959 for details.

Does mFTPClient enter FTP passive mode in retrieveFile? If not, you'll need enter passive mode before before calling retrieveFile or setup mFTPClient to enter passive mode in retrieveFile.
jiminaus is offline  
Reply With Quote
Old February 9th, 2012, 03:41 AM   #5 (permalink)
Junior Member
 
Valten1992's Avatar
 
Join Date: Feb 2012
Location: Ireland
Posts: 34
 
Device(s): Samsung Galaxy mini
Thanks: 5
Thanked 3 Times in 3 Posts
Default

I was working on it a bit more yesterday and noticed two things:

1.On the path I specify in the download method to download the file from the server to, an empty text file is created with the name of the file I want to to download. If I do not give a file name in the destination path I get a (Is a Directory error, which is caused by not specifying an actual file from what I understand)

2.My connection method causes the client to enter passive method after conecting to the server so it might not be that.

Here's my connection method:

Code:
public boolean ftpConnect(String host, String username,
            String password, int port)
    {
        try{
            mFTPClient = new FTPClient();
            // connecting to the host
            
                mFTPClient.connect(host, port);
            

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                /* Set File Transfer Mode
                 *
                 * To avoid corruption issue you must specified a correct
                 * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
                 * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
                 * for transferring text, image, and compressed files.
                 */
        
                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                mFTPClient.enterLocalPassiveMode();

                return status;
            }
        } catch(Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        return false;
    }
Valten1992 is offline  
Reply With Quote
Old February 9th, 2012, 04:04 AM   #6 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

What FTP client are you using?
jiminaus is offline  
Reply With Quote
Old February 9th, 2012, 04:27 AM   #7 (permalink)
Junior Member
 
Valten1992's Avatar
 
Join Date: Feb 2012
Location: Ireland
Posts: 34
 
Device(s): Samsung Galaxy mini
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by jiminaus View Post
What FTP client are you using?
Apache Commons
Valten1992 is offline  
Reply With Quote
Old February 9th, 2012, 05:12 AM   #8 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

With Apache Commons, you don't pass a full FTP URL as the remote parameter to retrieveFile. You only pass the remote path of the file.

Try changing:
Code:
   ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
to:
Code:
   ftpDownload("rooms.txt", f);
If rooms.txt isn't in the current working directory of the FTP session, you need to call changeWorkingDirectory on FTPClient to change directory before calling retrieveFile.


Also many of the Apache Commons Net library methods won't raise an exception if there's a protocol error, only if there's a failure on the communications link.

You need to check if the methods you're using are completing successfully and are returning true. If it returns false you need get the reply code and look up the meaning of the reply code in section 4.2 of RFC 959.
jiminaus is offline  
Last edited by jiminaus; February 9th, 2012 at 05:15 AM.
Reply With Quote
The Following User Says Thank You to jiminaus For This Useful Post:
Valten1992 (February 9th, 2012)
Old February 9th, 2012, 05:17 AM   #9 (permalink)
Junior Member
 
Valten1992's Avatar
 
Join Date: Feb 2012
Location: Ireland
Posts: 34
 
Device(s): Samsung Galaxy mini
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by jiminaus View Post
With Apache Commons, you don't pass a full FTP URL as the remote parameter to retrieveFile. You only pass the remote path of the file.

Try changing:
Code:
   ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
to:
Code:
   ftpDownload("rooms.txt", f);
If rooms.txt isn't in the current working directory of the FTP session, you need to call changeWorkingDirectory on FTPClient to change directory before calling retrieveFile.


Also many of the Apache Commons Net library methods won't raise an exception if there's a protocol error, only if there's a failure on the communications link.

You need to check if the methods you're using are completing successfully and are returning true. If it returns false you need get the reply code and look up the meaning of the reply code in section 4.2 of RFC 959.

Thats was it.......good god, that was it. So obvious I didnt consider to try it. Thank you very much
Valten1992 is offline  
Reply With Quote
Reply

Bookmarks

Tags
download, error-checking, ftp, log


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:26 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo