title: Uploading file to server in background
body: <p>I'm developing an app that allows to users to upload mediafiles(such as images, photos, videos) to server and I've got a little bit problem with large files - is too long to wait and view a progress bar in activity while its uploading and another problem - if app dies upload dies too so I need to transfer my uploading code to service. So here my troubles begins - if I'll transfer my upload code to server how can I send progress updates(%) to activity?</p>
  
  <p>here's my upload method's code:</p>
  
  <pre><code>public static void uploadMovie(final HashMap&lt;String, String&gt; dataSource, final OnResponseListener finishedListener, final ProgressListener progressListener) {
      if (finishedListener != null) {
          new Thread(new Runnable() {
              public void run() {
                  try {
  
                      //Prepare data--&gt;
  
                      String boundary = getMD5(dataSource.size() + String.valueOf(System.currentTimeMillis()));
                      MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
                      multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                      multipartEntity.setCharset(Charset.forName("UTF-8"));
                      for (String key : dataSource.keySet()) {
                          if (key.equals(MoviesFragmentAdd.USERFILE)) {
                              FileBody userFile = new FileBody(new File(dataSource.get(key)));
                              multipartEntity.addPart(key, userFile);
                              continue;
                          }
                          multipartEntity.addPart(key, new StringBody(dataSource.get(key), ContentType.APPLICATION_JSON));
                      }
                      HttpEntity entity = multipartEntity.build();
                      //&lt;--
  
                      //Prepare Connection--&gt;
  
                      trustAllHosts();
                      HttpsURLConnection conn = (HttpsURLConnection) new URL(SAKH_URL_API + "/video/addForm/").openConnection();
                      conn.setUseCaches(false);
                      conn.setDoOutput(true);
                      conn.setDoInput(true);
                      conn.setRequestMethod("POST");
                      conn.setRequestProperty("Accept-Charset", "UTF-8");
                      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                      conn.setRequestProperty("Content-length", entity.getContentLength() + "");
                      conn.setRequestProperty(entity.getContentType().getName(), entity.getContentType().getValue());
                      conn.connect();
  
  
                      //&lt;--
                      // Upload--&gt;
  
  
                      OutputStream os = conn.getOutputStream();
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                      entity.writeTo(baos);
                      baos.close();
                      byte[] payload = baos.toByteArray();
                      baos = null;
                      int totalSize = payload.length;
                      int bytesTransferred = 0;
                      int chunkSize = 2000;
  
                      while (bytesTransferred &lt; totalSize) {
                          int nextChunkSize = totalSize - bytesTransferred;
                          if (nextChunkSize &gt; chunkSize) {
                              nextChunkSize = chunkSize;
                          }
                          os.write(payload, bytesTransferred, nextChunkSize);
                          bytesTransferred += nextChunkSize;
  
                          //Progress update--&gt;
                          if (progressListener != null) {
                              progressListener.onProgressUpdate((100 * bytesTransferred / totalSize));
                          }
                          //&lt;--
  
                      }
  
                      os.flush();
  
                      //&lt;--
                      //Get server response--&gt;
                      int status = conn.getResponseCode();
                      if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
  
                          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                          JsonObject request = (JsonObject) gparser.parse(in.readLine());
  
                          if (!request.get("error").getAsBoolean()) {
                              finishedListener.onLoadFinished(new Object());
                          }
                      } else {
                          throw new IOException("Server returned non-OK status: " + status);
                      }
  
                      conn.disconnect();
                  } catch (Exception e) {
                      e.printStackTrace();
                      finishedListener.onNotConnected();
  
                  }
              }
          }).start();
  
      }
  }   
  </code></pre>