title: File download hanging
body: <p>after extensive searching I was unable to find a solution to solve a file download problem.</p>
  
  <p>The following script is designed to download a csv file from a remote (Hotmail) server via a
   <strong>WebViewClient</strong>.</p>
  
  <p>The login process is done through the their standard website but I would like to capture the downloaded csv file using the following download class and store in a custom location.</p>
  
  <p>It works fine with URLs that directly link to a file for eg, <strong>site.com/file.pdf</strong> but will not work on processed URLs such as <strong>site.com/downloadFile.php?n=xxxx</strong> where it just hangs until the <strong>connection is reset by the remote server</strong></p>
  
  <pre><code>    private class DownloadFile extends AsyncTask&lt;String, Integer, String&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          mProgressDialog.show();
      }
  
      @Override
      protected void onProgressUpdate(Integer... progress) {
          super.onProgressUpdate(progress);
          mProgressDialog.setProgress(progress[0]);
      }
  
       @Override
       protected void onPostExecute (String result){
  
       super.onPostExecute(result);
  
       mProgressDialog.dismiss();
       mProgressDialog = null;
       }
  
  
      @Override
      protected String doInBackground(String... sUrl) {
  
          try {
  
              Log.i("File download", "Started from :"+sUrl[0]);
  
              URL url = new URL(sUrl[0]);
              //URLConnection connection = url.openConnection();
              File myDir = new File(Environment.getExternalStorageDirectory() + "/myDir");
  
              // create the directory if it doesnt exist
              if (!myDir.exists()) myDir.mkdirs();            
  
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
  
              //Follow redirects so as some sites redirect to the file location
              connection.setInstanceFollowRedirects(true);        
              connection.setDoOutput(true);           
  
              connection.connect();
  
              File outputFile     =   new File(myDir, "hotmail_contacts.csv");
  
              // 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(outputFile);
  
              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);
              }
  
  
              connection.disconnect();
              output.flush();
              output.close();
              input.close();
  
              Log.i("File download", "complete");
  
          } catch (Exception e) {
  
              Log.e("File download", "error: " + e.getMessage());
  
          }
          return null;
      }
  }
  </code></pre>
  
  <p>The above AsyncTask is invoked in the <strong>onDownloadStart(....)</strong> method as follows:</p>
  
  <pre><code>public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long length) {
  
          Log.i("File download", "URL:" + url 
                  + " UserAgent:" + userAgent
                  + "ContentDisposition:" + contentDisposition 
                  + "Mime:"+ mimeType + "Length:" + Long.toString(length));
  
  
  
          // instantiate it within the onCreate method
          mProgressDialog = new ProgressDialog(Email_import.this);
          mProgressDialog.setMessage("File download");
          mProgressDialog.setIndeterminate(false);
          mProgressDialog.setMax(100);
          mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  
          // start a new download
          DownloadFile downloadFile = new DownloadFile();
          downloadFile.execute(url);
  
      }// end onCreate
  </code></pre>
  
  <p>All the relevant permissions are in the manifest such as write external storage, internet and read network state.</p>
  
  <p>Am I missing something here?
  Any help would be much appreciated</p>