title: Android : Is there any best way to download files in android ?"Connection time out exception while downloading large files?
body: <p>In my application i need to download loads of files around 800mb (max size of file). I had a listview , where each item contain download button where user can select any of downloads and one will be running other will be in queue.
  I am downloading files in following way ........</p>
  
  <pre><code>private boolean  Download1(String urlString, int sectionNo) {
  
          try {
  
              File file = new File(mFile,state.getAbbrevation()+"."+sectionNo+".zip");
              Log.v("Download", urlString);
              URL url = new URL(urlString);
              urlConnection = url.openConnection();
              inputStream = urlConnection.getInputStream();
  //          HttpClient client = new DefaultHttpClient();
  //          HttpGet request = new HttpGet(urlString);
  //          HttpResponse response = client.execute(request);
  //          BufferedReader bufferedReader = new BufferedReader(new InputStreamRead);
              byte[] buffer = new byte[1024];
              BufferedInputStream bis = new BufferedInputStream(
                      inputStream, 1024);
              ByteArrayBuffer baf = new ByteArrayBuffer(50);
              FileOutputStream fileOutputStream = new FileOutputStream(
                      file);
              int count = 0;
              while ((count = bis.read(buffer)) != -1) {
                  baf.append(buffer, 0, count);
                  currentByteCount += count;
                  if((currentByteCount - prevByteCount ) &gt; 1000000  ){
                  downloadTask.loadProgress(currentByteCount);
                  prevByteCount = currentByteCount;
                  }
              }
              fileOutputStream.write(baf.toByteArray());
              fileOutputStream.close();
  
          } catch (MalformedURLException e) {
  
              e.printStackTrace();
              handler.sendMessage(Message.obtain(handler, MSG_DOWNLOAD_FAILED, e.getMessage()));
              return false;
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Log.v(TAG, e.getMessage());
              handler.sendMessage(Message.obtain(handler, MSG_DOWNLOAD_FAILED, e.getMessage()));
              return false;
          } finally {
              try {
                  if(inputStream != null){
                  inputStream.close();
                  inputStream = null;
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return true;
      }
  </code></pre>
  
  <p>While downloading files some times am getting connection time out exception . Please can any one tell me how to avoid these exception ? Is there any better and fastest way to download files in android . If there is any such apis. Please let me know. </p>
  
  <pre><code>04-19 15:44:52.591: WARN/System.err(18199): java.net.SocketException: Connection timed out
  04-19 15:44:52.591: WARN/Smack/Packet(209): notify conn break (IOEx), close connection
  04-19 15:44:52.591: DEBUG/Smack(209): [XMPPConn] close connection, notifyClosed=false
  04-19 15:44:52.591: WARN/System.err(18199):     at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
  04-19 15:44:52.591: WARN/System.err(18199):     at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)
  04-19 15:44:52.591: WARN/System.err(18199):     at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
  04-19 15:44:52.591: WARN/System.err(18199):     at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
  04-19 15:44:52.591: WARN/System.err(18199):     at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:44)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.io.FilterInputStream.read(FilterInputStream.java:133)
  04-19 15:44:52.591: WARN/System.err(18199):     at com.logictreeit.pilot.utils.Download.Download1(Download.java:245)
  04-19 15:44:52.591: WARN/System.err(18199):     at com.logictreeit.pilot.utils.Download.access$4(Download.java:225)
  04-19 15:44:52.591: WARN/System.err(18199):     at com.logictreeit.pilot.utils.Download$DownloadTask.doInBackground(Download.java:171)
  04-19 15:44:52.591: WARN/System.err(18199):     at com.logictreeit.pilot.utils.Download$DownloadTask.doInBackground(Download.java:1)
  04-19 15:44:52.591: WARN/System.err(18199):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  04-19 15:44:52.591: WARN/System.err(18199):     at java.lang.Thread.run(Thread.java:1019)
  </code></pre>
  
  <p>Regards,
  Srinivas</p>