title: How to add parameters to HttpURLConnection url.openConnection()
body: <p>I am recording a video and uploading it to server on button click.
  At the same time I need to update a table in my database which has 2 columns, named "userId and filename".</p>
  
  <p>I followed a <a href="http://www.mayanklangalia.blogspot.in/2014/04/how-to-upload-image-audio-and-video-on.html" rel="nofollow noreferrer">tutorial</a> for uploading a video,now i need to pass the UserId to php page.I don't know how to do that.</p>
  
  <p>I found this <a href="https://stackoverflow.com/a/13486223/4361127">Answer</a> but it is giving IOException - stream closed</p>
  
  <p>Code snippet:</p>
  
  <p>private int uploadFile(String sourceFileUri)
      { </p>
  
  <pre><code>    HttpURLConnection conn = null;
      DataOutputStream dos = null;
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1024 * 1024;
  
      sourceFileUri = sourceFileUri.replace("file:///","/");
      File sourceFile = new File(sourceFileUri);
      Log.e("Image--sourceFile",""+sourceFile);
      Log.e("Image--sourceFileUri", "" + sourceFileUri);
  
      if (!sourceFile.isFile())
      {
          dialog.dismiss();
          Log.e("uploadFile", "Source File not exist :" + fileUri);
          runOnUiThread(new Runnable() {
              public void run() {
                //  txtPercentage.setText("Source File not exist :" + fileUri);
              }
          });
  
          return 0;
  
      } else {
          try {
              FileInputStream fileInputStream = new FileInputStream(
                      sourceFile);
              URL url = new URL(upLoadServerUri);
              conn = (HttpURLConnection) url.openConnection();
              conn.setDoInput(true); // Allow Inputs
              conn.setDoOutput(true); // Allow Outputs
              conn.setUseCaches(false); // Don't use a Cached Copy
              conn.setRequestMethod("POST");
              conn.setRequestProperty("Connection", "Keep-Alive");
              conn.setRequestProperty("ENCTYPE", "multipart/form-data");
              conn.setRequestProperty("Content-Type",
                      "multipart/form-data;boundary=" + boundary);
              conn.setRequestProperty("uploaded_file", sourceFileUri);
  
  
              /* List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("UserId", UserId));
  
              OutputStream os = conn.getOutputStream();
              BufferedWriter writer = new BufferedWriter(
                      new OutputStreamWriter(os, "UTF-8"));
              writer.write(getQuery(params));
              writer.flush();
              writer.close();
              os.close();
  
              conn.connect();  */
  
              dos = new DataOutputStream(conn.getOutputStream());
  
              dos.writeBytes(twoHyphens + boundary + lineEnd);
              dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                      + sourceFileUri + "\"" + lineEnd);
  
              dos.writeBytes(lineEnd);
  
              // create a buffer of maximum size
              bytesAvailable = fileInputStream.available();
  
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              buffer = new byte[bufferSize];
  
              // read file and write it into form...
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
              while (bytesRead &gt; 0) {
  
                  dos.write(buffer, 0, bufferSize);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
              }
  
              // send multipart form data necesssary after file data...
              dos.writeBytes(lineEnd);
              dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
              // Responses from the server (code and message)
              serverResponseCode = conn.getResponseCode();
              String serverResponseMessage = conn.getResponseMessage();
  
              Log.i("uploadFile", "HTTP Response is : "
                      + serverResponseMessage + ": " + serverResponseCode);
  
              if (serverResponseCode == 200) {
  
                  runOnUiThread(new Runnable() {
                      public void run() {
                          Toast.makeText(Video.this,
                                  "File Upload Complete.", Toast.LENGTH_SHORT)
                                  .show();
                      }
                  });
              }
  
              // close the streams //
              fileInputStream.close();
              dos.flush();
              dos.close();
  
          } catch (MalformedURLException ex) {
  
              dialog.dismiss();
              ex.printStackTrace();
  
              runOnUiThread(new Runnable() {
                  public void run() {
                      Toast.makeText(Video.this,
                              "MalformedURLException", Toast.LENGTH_SHORT)
                              .show();
                  }
              });
  
              Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
          } catch (Exception e) {
  
              dialog.dismiss();
              e.printStackTrace();
  
              runOnUiThread(new Runnable() {
                  public void run() {
                      Toast.makeText(Video.this,"Something went wrong", Toast.LENGTH_SHORT).show();
                  }
              });
              Log.e("UploadingFile Exception","" + e.getMessage(), e);
          }
          dialog.dismiss();
          return serverResponseCode;
      }
  }
  </code></pre>