Pregunta

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();
        }
    }
¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top