0

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

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();
        }
    }
Amanni
  • 1,924
  • 6
  • 31
  • 51

2 Answers2

0

Are you sure the server is responding in less then 15 sec?(that is the timeout I've seen that you have set). 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.

bogdan
  • 782
  • 3
  • 7
  • Thanks this only seems to be available for API 9 and above – Amanni Jul 08 '13 at 10:13
  • If you are sure the server is working, you can set the timeout to 0 (this means no timeout) – bogdan Jul 08 '13 at 10:55
  • The recommended way to do this is to check the api version of the device, if it is 9 or above you should you the download manager, if it is 8, use the manual download. – bogdan Jul 08 '13 at 11:45
  • can i use the download manager to rename files as they are downloaded and also does it take an array on urls? – Amanni Jul 08 '13 at 13:34
  • Search a bit on google, for renaming files, take a look at `.setDestinationInExternalFilesDir()` (this is on the request) and it doesn't take an array of urls as far as I know, but you can make multiple requests for downloads. – bogdan Jul 09 '13 at 08:22
0

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.

  • what is the maximum timeout possible, I increased it to 2 mins and seemed to work better but I'd like to be at Max. – Amanni Jul 08 '13 at 10:27
  • I'm not using any download manager, is there one available for API level 8? – Amanni Jul 08 '13 at 10:28
  • I couldn't find a DM for API but I did find a question that has been answered for a DM for API. [quote] _How_ _can_ _i_ _use_ _DownloadManager_ _in_ _API_ _levels_ _lower_ _than_ _9?_ You can't. You will need to download the file yourself, using HttpUrlConnection or HttpClient[/quote –  Jul 08 '13 at 10:38