简体   繁体   English

下载文件时,Android应用会不定期地冻结

[英]Android app freezes irregularly when downloading files

I'm making an Android app where the user can download files from a FTP-server. 我正在制作一个Android应用程序,用户可以在其中从FTP服务器下载文件。 For the ftp parts I am using apache.org.commons-net package. 对于ftp部分,我正在使用apache.org.commons-net软件包。

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 %). 我遇到的问题是,如果服务器上有6个文件,并且在模拟器上运行此代码,它将下载前两个文件,然后冻结(进度条挂在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 在此先感谢,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();
        }
    }
};

OK, found the problem: OK,发现问题:

When the boolean downloadIsNewer is false I do not call client.completePendingCommand(). 当boolean downloadIsNewer为false时,我不会调用client.completePendingCommand()。 When I add that before the line is.close() it works like a charm. 当我在is.close()行之前添加它时,它就像一个符咒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当浏览器或某些应用开始下载文件时,如何在Android中收听? - How to listen in Android when the browser or some app started downloading a files? 当应用程序在后台没有充电器时,可以在Android上下载文件吗? - Is downloading files on Android possible when the app is in background without a charger? 从android中的服务器下载内容时屏幕冻结 - Screen freezes when downloading content from server in android Android:使用处理程序和切换时应用程序冻结 - Android: App freezes when using handler and switch Android,当屏幕方向改变时,应用会冻结 - Android, When the screen orientation changes, the app freezes 从Web View Android应用下载文件 - Downloading files from Web View Android app Firebase实施会在下载数据时冻结应用程序 - Firebase Implementation freezes app while downloading data 显示或隐藏应用软键盘时,Android应用冻结 - Android app freezes when app soft keyboard is displayed or hide Android startactivity 冻结应用程序 - Android startactivity freezes the app 尝试使用SQLite数据库时Android应用程序冻结 - Android app freezes when trying to use an SQLite database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM