简体   繁体   English

下载多个文件不断停止android

[英]download multiple files keeps stopping android

Trying to download about 38 video files from a server with the code below and for some reason it keeps stopping at different points during the download, I'm mostly getting a 尝试使用下面的代码从服务器上下载约38个视频文件,由于某种原因,它在下载过程中一直停在不同的位置,我大多

java.net.SocketException: Connection timed out

I'd like to know how I can perform this with less errors 我想知道如何减少错误

My code below 我的代码如下

private class DownloadFile extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            mProgressDialog.setProgress(progress[0]);
            mProgressDialog.setMessage("Downloading "+(i+1)+" of "+downloadURL.length);
        }

        @Override
        protected String doInBackground(String... sUrl) {
            try {

                for(int i = 0; i < sUrl.length; i++){

                    URL url = new URL("http://myvideo.info/videos/"+sUrl[i]);
                    URLConnection connection = null;
                    try {

                        connection = url.openConnection();
                        connection.setConnectTimeout(15000);
                        connection.setReadTimeout(15000);
                    } catch (java.net.SocketTimeoutException e) {
                        e.printStackTrace();
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                    connection.connect();
                    // this will be useful so that you can show a typical 0-100% progress bar
                    int fileLength = connection.getContentLength();

                    // download the file
                    InputStream input = new BufferedInputStream(url.openStream());
                    OutputStream output = new FileOutputStream("/sdcard/"+file_rename[i]);

                    byte data[] = new byte[1024];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        // publishing the progress....
                        publishProgress((int) (total * 100 / fileLength));
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();
                }

            } catch (Exception e) {
                Log.e("PP", "PP", e);
            }
            return null;
        }

        protected void onPostExecute(String jsonResult) {
            mProgressDialog.dismiss();
        }
    }

Are you sure the server is responding in less then 15 sec?(that is the timeout I've seen that you have set). 您确定服务器的响应时间不到15秒?(这是我所看到的已设置的超时时间)。 If the files are big you should be downloading them separately, take a look at Downloader manager , you can use it to download big files easy. 如果文件很大,您应该单独下载它们,请查看Downloader Manager ,您可以使用它轻松下载大文件。

What download manager are you using? 您正在使用什么下载管理器? And I'd suggest changing your timeout to the maximum. 我建议您将超时时间更改为最大。 Personally your code seems fine. 就个人而言,您的代码看起来还不错。 I think it would be your download manager and timeout. 我认为这将是您的下载管理器和超时时间。 Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM