Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5842117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:58:34+00:00 2026-05-22T11:58:34+00:00

I’m making an Android app where the user can download files from a FTP-server.

  • 0

I’m making an Android app where the user can download files from a FTP-server. For the ftp parts I am using apache.org.commons-net package.

When I have connected to server I get a list of the filenames, and then I want to download each file. I start the download routine which runs in a thread of it’s own.

The problem I’m experiencing is, that if I have say 6 files on the server, and I run this code on my emulator, it will download the first two files, and then just freeze (with the progressbar hanging on 34 %). When I run it on my phone it will download three files and freeze.

If I debug my way through the code on the emulator it will download all six files just fine and not freeze.

Does anyone have any idea what might be the problem?

Thanks in advance,
LordJesus

This is my code (the client is already initialized):

private void downloadFiles2() {
    dialog = new ProgressDialog(this);
    dialog.setCancelable(true);
    dialog.setMessage("Loading...");
    // set the progress to be horizontal
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    // reset the bar to the default value of 0
    dialog.setProgress(0);
    dialog.setMax(DownloadCount);
    // display the progressbar
    dialog.show();

    // create a thread for updating the progress bar
    Thread background = new Thread (new Runnable() {
        public void run() {
            InputStream is = null; 
            try {
                for (String filename : fileNames) {
                    is = client.retrieveFileStream(filename); 
                    byte[] data = new byte[1024];
                    int x = 0;
                    x = is.read(data, 0, 1024);
                    boolean downloadIsNewer = true;
                    File fullPath = new File(path + "/" + filename); 
                    Log.d("FTP", "Starting on " + filename);
                    if (fullPath.exists()) {
                        downloadIsNewer = checkIfNewer(data, fullPath);
                    }

                    if (downloadIsNewer) {
                        Log.d("FTP", "Need to download new file");
                        FileOutputStream fos = new FileOutputStream(fullPath);
                        fos.write(data,0,x); 
                        while((x=is.read(data,0,1024))>=0){
                            fos.write(data,0,x); 
                        }
                        fileText += filename + " - downloaded OK." + FileParser.newline;
                        is.close();
                        client.completePendingCommand();
                        fos.flush();
                        fos.close();                        
                    }
                    else {
                        Log.d("FTP", "No need to download");
                        is.close();
                        fileText += filename + " - own copy is newer." + FileParser.newline;
                    }
                    // active the update handler
                    progressHandler.sendMessage(progressHandler.obtainMessage());
                }
                client.logout();
                client.disconnect();
            }

            catch (Exception e) {
                // if something fails do something smart
            }
        }
    });
    // start the background thread
    background.start();
}

Handler progressHandler = new Handler() {  
    public void handleMessage(Message msg) {
        dialog.incrementProgressBy(1);
        InfoTextView.setText(fileText);
        if(dialog.getProgress()== dialog.getMax())
        {

            dialog.dismiss();
        }
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:58:34+00:00Added an answer on May 22, 2026 at 11:58 am

    OK, found the problem:

    When the boolean downloadIsNewer is false I do not call client.completePendingCommand(). When I add that before the line is.close() it works like a charm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.