title: AsyncTask unable to download file from external site
body: <p>I'm working on my first Android app but I can't seem to make it work properly. This app should download, on click, a file (a pdf, to be specific) from a site (in this case, my personal site) and save it on the SDCard.
  I looked around here and found that I should use an AsyncTask for this purpose and this is the code I came up with:</p>
  
  <pre><code>package it.uniroma3.tirocinioandroid;
  
  import java.io.BufferedInputStream;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.URL;
  import java.net.URLConnection;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  
  public class MainActivity extends Activity implements OnClickListener {
  
      private Button dlbutton;
      private ProgressDialog mProgressDialog;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          dlbutton = (Button) findViewById(R.id.dlbutton);
          dlbutton.setOnClickListener(this);
          mProgressDialog = new ProgressDialog(MainActivity.this);
          mProgressDialog.setMessage("Currently downloading...");
          mProgressDialog.setIndeterminate(false);
          mProgressDialog.setMax(100);
          mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
          Log.i("Main", "finito onCreate");
  
      } 
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          //TODO sistemare il menù
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  
      @Override
      public void onClick(View v) {
          switch (v.getId())
          {
          case R.id.dlbutton:
              DownloadFile downloadFile = new DownloadFile();
              downloadFile.execute("http://dragon-nest.net/catalogo.pdf");
              Log.i("Main", "finito onClik");
              break;
          }
  
      }
  
      private class DownloadFile extends AsyncTask&lt;String, Integer, String&gt; {
          @Override
          protected String doInBackground(String... sUrl) {
              try {
                  URL url = new URL(sUrl[0]);
                  URLConnection connection = url.openConnection();
                  connection.connect();
                  Log.i("Main", "connected");
                  // 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());
                  Log.i("Main", "input set");
                  OutputStream output = new FileOutputStream(Environment.getDataDirectory().getAbsolutePath().concat("download/catalogo.pdf"));
                  Log.i("Main", "output set");
  
                  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("ERROR_TAG", e.getMessage());
                      return "fine";
                  }
              String s ="fine";
              Log.i("Main", "end download");
              return s;
          }
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              mProgressDialog.show();
              Log.i("Main", "end preExecute");
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              mProgressDialog.dismiss();
              Log.i("Main", "end postExecute");
          }
  
          @Override
          protected void onProgressUpdate(Integer... progress) {
              super.onProgressUpdate(progress);
              mProgressDialog.setProgress(progress[0]);
              Log.i("Main", "update...");
          }
  
  
      }
  }
  </code></pre>
  
  <p>However, it seems to be unable to connect to the site to begin the download. From the Logcat it shows that it generates an exception when it tries to connect which is caught and blocks everything before the download can really begin.</p>
  
  <p>Here is also the manifest for the app:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="it.uniroma3.tirocinioandroid"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;user-permission android:name="android.permission.INTERNET" /&gt;
      &lt;user-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="it.uniroma3.tirocinioandroid.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="it.uniroma3.tirocinioandroid.FileDownloader"
              android:label="@string/title_activity_file_downloader" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>By the way, I tried to see if maybe it was my site acting funky, but I can download the file from the browser just fine. I also tried with another file from another site but I get the same problem.
  This happens to me both on the AVD and my tablet so I don't think it's an AVD-related issue.</p>