title: Android: Complete file not downloaded from url
body: <p>In my app, I have requirement to download mp3 files from url. I am downloading file using Async Task. For testing purpose, I have kept file in dropbox. The problem is, it is not downloading complete file. The actual file size is of 5 MB. But, it is downloading only 29 KB of data. When I checked the length of content, it showed -1. I am not getting where is the problem. Below, I am posting my code.</p>
  
  <pre><code>@Override
      protected Void doInBackground(String... sUrl) 
      {
          InputStream input = null;
          OutputStream output = null;
          try 
          {
              URL link = new URL(sUrl[0]);
              HttpURLConnection urlConnection = (HttpURLConnection)link.openConnection();
              urlConnection.setRequestMethod("GET");
              urlConnection.setDoOutput(true);
              urlConnection.connect();
              int fileLength = urlConnection.getContentLength();
              Log.v("Download file length", ""+fileLength);
              input = urlConnection.getInputStream();
              int downloadedSize = 0;
              output = new FileOutputStream(Environment.getExternalStorageDirectory()
                      .getAbsolutePath()+"/audiofolder/1.mp3.mp3");
  
              byte[] data = new byte[1024];
              int bufferLength = 0;
  
              while (((bufferLength = input.read(data)) &gt; 0)) 
              {
                  output.write(data, 0, bufferLength);
                  downloadedSize += bufferLength;
                  publishProgress((int)((downloadedSize/fileLength) * 100));
              }//while
              output.flush();
              output.close();
              input.close();
          }//try 
          catch (MalformedURLException e) 
          {
              e.printStackTrace();
          }//catch
          catch (IOException e) 
          {
              e.printStackTrace();
          }//catch
          return null;
      }//diInBackground
  </code></pre>