title: I want to upload files one by one like queue in android
body: <p>I want to upload files one by one just like queue. In brief suppose there are 10 files and I want to upload all files and it should be checking for network connectivity is available or not, if not then those file should be into the queue and will try to upload later. It will keep on trying till any single file in left queue. Please help me out, How can I achieve this in android.</p>
  
  <p>This is the sample code that I have tried so far</p>
  
  <pre><code>final Thread thread = new Thread(){
          public void run() {
              try {
                  while(true){
                      ThisCaching ic = new ThisCaching(getApplicationContext());
                      fileToUpload = ic.getDataFromCache("audiofiles", "SELECT * FROM audiofiles WHERE STATUS = '2' OR STATUS = '3' ORDER BY rowid DESC");
                      if(fileToUpload != null)
                      {
                          for (int i=0; i&lt;fileToUpload.size(); i++) {
                              try {
                                  System.out.println("Current position while uploading ::&gt; " + i);
                                  Thread.sleep(1000);
                                  ConstantData.selectedFile = fileToUpload.get(i).toString();
                                  ConstantData.position = i;
                                  if(checkConnectivity()){
                                      new Uploading().execute();
                                      myApplication.getHttpClient().getConnectionManager().closeExpiredConnections();
                                  }else{
                                      String[] status = {"STATUS"};
                                      String[] val = {"2"};
                                      try {
                                          SQLiteDatabase sd = ThisDataHelper.getInstance(getApplicationContext()).getDB();
                                          new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
                                      } catch (Exception e1) {
                                          e1.printStackTrace();
                                      }
                                  }
                              } catch (Exception e) {
                                  return;
                              }            
                          }
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
                  return;
              }            
          }
      };
  
  
          String line = null;
          HttpClient httpClient = null;
          HttpContext localContext = null;
          HttpPost httpPost = null;
          MultipartEntity entity = null;
          HttpResponse response = null;
          HttpEntity httpEntity =null;
          SQLiteDatabase sd = ThisDataHelper.getInstance(getApplicationContext()).getDB();
          try {
              String strArray[]={"http://myurl.com","filename",ConstantData.selectedFile,"activityType",ConstantData.activityType,"patientId",ConstantData.patientId,"caseId",ConstantData.caseId,"doctorId",ConstantData.doctorId,"clinicCode",ConstantData.clinicCode,"documentType",ConstantData.documentType,"templateId",ConstantData.templateId};
              httpClient = new DefaultHttpClient();
              //httpClient.getConnectionManager().r
              localContext = new BasicHttpContext();
              httpPost = new HttpPost(strArray[0]);
              entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
              File file = new File(ConstantData.selectedFile);
              String type = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(".")+1);
              if(strArray.length&gt;1)
              {
                  for(int i=1;i&lt;strArray.length;i=i+2)
                  {
                      if(strArray[i]!=null){
                          if(!strArray[i].equalsIgnoreCase("")){
                              //                              System.out.println("i) : "+i +"  i+1) : "+(i+1));
                              Log.i(strArray[i], "::" + strArray[i+1]);
                              if(strArray[i].equalsIgnoreCase("filename")){
                                  entity.addPart("filename", new FileBody(file,"audio/"+type));
                              }else{
                                  entity.addPart(strArray[i], new StringBody(strArray[i+1]));
                              }
                          }
                      }
                  }
              }
              httpPost.setEntity(entity);
              response = httpClient.execute(httpPost,localContext);// i m getting error at this line
  
              System.out.println("Response code is ::&gt; " +response.getStatusLine().getStatusCode());
              code = response.getStatusLine().getStatusCode();
              httpEntity = response.getEntity();
              line = EntityUtils.toString(httpEntity, HTTP.UTF_8);
          } catch (UnsupportedEncodingException e) {
              code=100;
              e.printStackTrace();
              line = "&lt;results status=\"error\"&gt;&lt;msg&gt;Can't connect to server&lt;/msg&gt;&lt;/results&gt;";
              ConstantData.uploaded = false;
              String[] status = {"STATUS"};
              String[] val = {"3"};
              try {
                  new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          } catch (MalformedURLException e) {
              code=100;
              e.printStackTrace();
              line = "&lt;results status=\"error\"&gt;&lt;msg&gt;Can't connect to server&lt;/msg&gt;&lt;/results&gt;";
              ConstantData.uploaded = false;
              String[] status = {"STATUS"};
              String[] val = {"3"};
              try {
                  new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          } catch (IOException e) {
              code=100;
              e.printStackTrace();
              line = "&lt;results status=\"error\"&gt;&lt;msg&gt;Can't connect to server&lt;/msg&gt;&lt;/results&gt;";
              ConstantData.uploaded = false;
              String[] status = {"STATUS"};
              String[] val = {"3"};
              try {
                  new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          } catch (Exception e) {
              code=100;
              e.printStackTrace();
              ConstantData.uploaded = false;
              String[] status = {"STATUS"};
              String[] val = {"3"};
              try {
                  new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          }finally{
  //  
  
  
  try {
  //                   System.out.println("&amp;&amp;&amp;&amp; while realeasing connection");
  ////                     if(httpClient != null){
  ////                         httpClient.getConnectionManager().releaseConnection((ManagedClientConnection) httpClient.getConnectionManager(), -1, TimeUnit.MINUTES);
  ////                     }
  //                   if(httpPost.getEntity().getContent() != null){
  //                       InputStream content = httpPost.getEntity().getContent();
  //                       content.close(); 
  //                   }
  //              } catch (Exception e) {
  //                  e.printStackTrace();
  //              }
              }
  
              return line;
  
  
  `
  </code></pre>