title: A good approach to do multipart file upload in Android
body: <p>I am working on a piece of code to do multipart form data POST request, which in my case is just to upload an image to server with parameters. Here's what I have now:</p>
  
  <p>I have a button to trigger the multipart request, in the button OnClickListener, I have this code to spin a new thread:</p>
  
  <pre><code>new Thread(new Runnable(){
  
  @Override
  public void run() {
  
      String photoUri = getPhotoUri();
      String url = getEndPointUrl();
  
      try {   
  
      NewPostRequest.postFile(url, photoUri, &lt;Other Params...&gt;);
  
      } catch (Exception e) {
          // Exception Handling           
      } 
  }).start();
  </code></pre>
  
  <p>And the <code>NewPostRequest.postFile</code> is just using <strong>Apache Http Client</strong> to make a request, basically like below:</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
  
  HttpPost post = new HttpPost(url);
  
  MultipartEntityBuilder builder = MultipartEntityBuilder.create();    
  
  builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  
  File file = new File(fileUri);
  
  FileBody fb = new FileBody(file);
  
  builder.addPart("file", fb);
  
  builder.addTextBody("param", otherParam);
  
  HttpEntity entity = builder.build();
  
  post.setEntity(entity);
  
  HttpResponse response = client.execute(post);      
  </code></pre>
  
  <p>I need to spin a new thread everytime because the recent Android releases doesn't let program to make http requests on UI thread. However, I really against to spin a random thread and let it out of control like the code above. I have tried to use <a href="https://developers.google.com/events/io/sessions/325304728" rel="nofollow">Google Volley</a> library, but it is not a handful tool when uploading large data files like image.</p>
  
  <p>I was wondering what I should do to make this call more manageable?</p>
  
  <p>===== UPDATE =====</p>
  
  <p>I switched to use <strong>AsyncTask</strong> and it works OK for now. I will keep this question open to see if any one has better approach.</p>