title: Using HttpPost to upload file Android
body: <p>am I uploading this file right? Here is my code:</p>
  
  <hr>
  
  <h2>Deleted Code</h2>
  
  <p>it is basically just a async task that sends the file (csv) to the web server. </p>
  
  <p>I am getting back status code 400 :( Anyone know what I am doing wrong?</p>
  
  <hr>
  
  <p>Edit: Now I am getting back status code 411, but when I specify the content-length it comes back with ClientProtocolException.</p>
  
  <p>Here's my code now:</p>
  
  <pre><code>UploadTask uploadtask;
  
      public class UploadTask extends AsyncTask&lt;Void, byte[], Boolean&gt; {
  
          HttpPost httppost;
  
          @Override
          protected Boolean doInBackground(Void... params) {
              Boolean result = false;
              String id = projectIDs.get((int) spinner.getSelectedItemId());
  
              Log.i("TAG", id);
              try {
  
                  l(send(String.valueOf(id), "http://" + site
                          + "/restlet/position.csv?project=" + id,
                          "/csv.csv"));
  
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              return result;
          }
  
          private void entity(String id, String file) throws JSONException,
                  UnsupportedEncodingException, FileNotFoundException {
              // Add your data
              File myFile = new File(Environment.getExternalStorageDirectory(),
                      file);
  
              FileEntity fileEntity = new FileEntity(myFile, "multipart/form-data;");
              fileEntity.setChunked(true);
              long len = fileEntity.getContentLength();
              httppost.getParams().setParameter("project", id);
              httppost.setEntity(fileEntity);
  
              //httppost.addHeader("Content-Length",String.valueOf(len));
              httppost.setHeader("Content-Length", String.valueOf(len));
          }
  
          private String send(String id, String URL, String file)
                  throws ClientProtocolException, IOException, JSONException {
  
              l(URL);
  
              HttpResponse response = null;
  
              httppost = new HttpPost(URL);
  
              entity(id, file);
  
              response = httpclient.execute(httppost);
  
              Header[] head = response.getAllHeaders();
  
              String str = String.valueOf(response.getStatusLine()
                      .getStatusCode());
  
              response.getEntity().consumeContent();
  
              return str;
          }
  
      }
  </code></pre>