title: Android AsyncTask Upload Files
body: <p>I want upload a number of files that are stored on the android device but it seems to be stuck in the doInBackground Method of AsyncTask. I get no errors. The Dialog popsup and stays active i eliminated the dialog and no effect. The other part of my project is to decode the json files I uploaded and store them in a database but thats for later. </p>
  
  <pre><code>    /********************UPLOAD GPSDATA*************************************/
  class UploadGpsData extends AsyncTask&lt;Void, Void, Void&gt;{
  
      NetworkInfo net;
  
      MainActivity uActivity;
  
      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;
      DataInputStream inputStream = null;
  
      String folderPath;
      String arrayOfFiles[];
      File root;
      File allFiles;
  
      String urlServer = "http://urluploadscriptaddress.php";
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary =  "*****";
  
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1*1024*1024;
  
      URL url;
  
       ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
  
  
  
      @Override
      protected void onPreExecute() {
  
  
          Log.d(" UploadGpsData","onPreRequest");
  
              pDialog.setMessage("Uploading GPS Data. Please wait...");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(true);
              pDialog.show();
  
  
      }
  
      @Override
      protected Void doInBackground(Void... params) {
  
           Log.d(" UploadGpsData","doInBackground");
  
          root = Environment.getExternalStorageDirectory(); 
  
          //pathToOurFile = root.getAbsolutePath()+"/Beagle Data/07-09-2013_16-21-30.json";
  
          folderPath = root.getAbsolutePath()+"/Beagle Data/";
  
          allFiles = new File(folderPath);
  
          arrayOfFiles = allFiles.list(); 
  
  
  
  
          for(int i = 0; i &lt; arrayOfFiles.length; i++){
  
              Log.d("File Names", arrayOfFiles[i].toString());
              //File filename = new File(arrayOfFiles[i].toString());
              try {
                  url = new URL(urlServer);
              } catch (MalformedURLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              try {
                  connection = (HttpURLConnection) url.openConnection();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              // Allow Inputs &amp; Outputs
              connection.setDoInput(true);
              connection.setDoOutput(true);
              connection.setUseCaches(false);
  
              // Enable POST method
              try {
                  connection.setRequestMethod("POST");
              } catch (ProtocolException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              connection.setRequestProperty("Connection", "Keep-Alive");
              connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
              try{
  
              FileInputStream fileInputStream = new FileInputStream(new File(folderPath+arrayOfFiles[i].toString()) );
              try {
                  outputStream = new DataOutputStream( connection.getOutputStream() );
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              outputStream.writeBytes(twoHyphens + boundary + lineEnd);
              outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + folderPath+arrayOfFiles[i].toString() +"\"" + lineEnd);
              outputStream.writeBytes(lineEnd);
  
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              buffer = new byte[bufferSize];
  
              // Read file
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
              while (bytesRead &gt; 0){
                  outputStream.write(buffer, 0, bufferSize);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
              }
  
              outputStream.writeBytes(lineEnd);
              outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
              //int serverResponseCode = connection.getResponseCode();
              //String serverResponseMessage = connection.getResponseMessage();
  
              // Responses from the server (code and message)
              //serverResponseCode = connection.getResponseCode();
              //serverResponseMessage = connection.getResponseMessage();
  
              fileInputStream.close();
              outputStream.flush();
              outputStream.close();
              } catch(Exception e){
                  e.printStackTrace();
              }
  
          }
  
  
      return null;
      }
  
      protected void onPostExecute() {
  
           Log.d(" UploadGpsData","onPost");
  
          pDialog.dismiss();
  
          txtUploadStatus = (TextView) findViewById(id.txtUploadStatus);
  
          txtUploadStatus.setText("Upload Achieved");
      }
  }
  /********************END OF UPLOADGPSDATA*************************************/
  </code></pre>
  
  <p>PHP Script below: The error log is empty so I am assuming its stuck on the android application</p>
  
  <pre><code>&lt;?php
  $target_path  = "./";
  
  $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
  
  $file = basename( $_FILES['uploadedfile']['name']);
  
  move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
  ?&gt;
  </code></pre>