title: Android: Uploading a file to a page along with other POST strings
body: <p>I am trying to upload an image to a PHP page along with some other info about the image so the PHP page knows what to do with it. Currently, I am getting away with it using this:</p>
  
  <pre><code>URL url = new URL("http://www.tagverse.us/upload.php?authcode="+WEB_ACCESS_CODE+"&amp;description="+description+"&amp;userid="+userId);
  HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setDoOutput(true);
  connection.setRequestMethod("POST");
  
  OutputStream os = connection.getOutputStream();
  InputStream is = mContext.getContentResolver().openInputStream(uri);
  BufferedInputStream bis = new BufferedInputStream(is);
  int totalBytes = bis.available();
  
  for(int i = 0; i &lt; totalBytes; i++) {
      os.write(bis.read());
  }
  os.close();
  
  BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  String serverResponse = "";
  String response = "";
  
  while((response = reader.readLine()) != null) {
      serverResponse = serverResponse + response;
  }
  reader.close();
  bis.close();
  </code></pre>
  
  <p>Is there a more elegant solution to this besides having a GET/POST hybrid? I feel as if this is sloppy, but for all I know it is a perfectly acceptable solution. If there is a better way of doing this, I'd appreciate being pointed in the right direction. Thanks!</p>
  
  <p>PS: I am familiar with how you would, under normal conditions, interact with a PHP page via POST:</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost("http://www.tagverse.us/login.php");
  
  try {
      List&lt;NameValuePair&gt; pairs = new ArrayList&lt;NameValuePair&gt;();
      pairs.add(new BasicNameValuePair("authcode", WEB_ACCESS_CODE));
      pairs.add(new BasicNameValuePair("username", username));
      pairs.add(new BasicNameValuePair("password", password));
      post.setEntity(new UrlEncodedFormEntity(pairs));
      client.execute(post);
  }
  </code></pre>
  
  <p>Essentially what I'd like to do is combine these two methods, but because I work with an HttpURLConnection object rather than a HttpPost object, it isn't as simple as just merging the two.</p>
  
  <p>Thank you!</p>