0

UPDATE answer is here :) https://stackoverflow.com/a/23648537/6042879

i want to be able to upload my video and a variable to my server to use in the PHP script.

So far i can choose the video i want from my phone and upload it perfectly fine to the server but cant figure out exactly how to send the variable with the video. I can do them separately but it wont work if i combine them.

i use this code to use to upload variables:

//Uploads the product details
        try {//Try block is to see if the call to the database can work.
            URL url = new URL(ProductDetails_URL);//Create a new URL and put there variable "register_URL" into it.
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//Create a httpConnection and open it
            httpURLConnection.setRequestMethod("POST");//Use the request method
            httpURLConnection.setDoOutput(true);

            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

            String data = URLEncoder.encode("ProductOwnerEmail", "UTF-8") + "=" + URLEncoder.encode(ProductOwnerEmail, "UTF-8") + "&" +
          URLEncoder.encode(DescriptionPoint3, "UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();

            InputStream IS = httpURLConnection.getInputStream();
            IS.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) { //url.openConnection() catch statement
            e.printStackTrace();
        }

Video upload code:

try{
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(UploadVideo_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                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("myFile", selectedPath);
                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + selectedPath + "\"" + lineEnd);
                dos.writeBytes(lineEnd);






                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data;      name=\"Details\";Email=\"" + ProductOwnerEmail + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.write(ProductOwnerEmail.getBytes());
                dos.writeBytes(lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"Details\";KeyCode=\"" + ProductKeyCode + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.write(ProductOwnerEmail.getBytes());
                dos.writeBytes(lineEnd);








                bytesAvailable = fileInputStream.available();
                Log.i("Huzza", "Initial .available : " + bytesAvailable);

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                serverResponseCode = conn.getResponseCode();

                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (Exception e) {
                e.printStackTrace();
                return "Product upload failed";
            }

PHP code:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$file_name = $_FILES['myFile']['name'];
$file_size = $_FILES['myFile']['size'];
$file_type = $_FILES['myFile']['type'];
$temp_name = $_FILES['myFile']['tmp_name'];






$ProductOwnerEmail = $_FILES['Details']['Email'];
$ProductKeyCode = $_FILES['Details']['KeyCode'];

$NewDirectory = "/var/www/html/ProductVideos/" . $ProductOwnerEmail;

if (!file_exists($NewDirectory))
{
    mkdir($NewDirectory, 0777, true);
}






$location = "/var/www/html/ProductVideos/$ProductOwnerEmail/" .    $ProductKeyCode;//$NewDirectory . '/' . $file_name;

move_uploaded_file($temp_name, $location);
echo   "Uploaded!";
}else{
echo "Error";
}
?>
Community
  • 1
  • 1
Lazar Kukolj
  • 696
  • 3
  • 15
  • 43

2 Answers2

1

After you code:

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + SelectedPDF + "\"" + lineEnd);
dos.writeBytes(lineEnd);

Write something like below to send variables:

dos.writeBytes(LINE_END);

// Loop a list of variable that you want to send to server.
/*for (StringKeyValuePair pair : yourVariableList) {
    dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
    dos.writeBytes("Content-Disposition: form-data; name=\"" + pair.getKey()+  "\"" + LINE_END);
    dos.writeBytes(LINE_END);
    dos.write(pair.getValue().getBytes());
    dos.writeBytes(LINE_END);
}*/

dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"ProductOwnerEmail\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(ProductOwnerEmail.getBytes());
dos.writeBytes(LINE_END);

dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"ProductKeyCode\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(ProductKeyCode.getBytes());
dos.writeBytes(LINE_END);
Joey Chong
  • 1,470
  • 15
  • 20
  • As what I mention, StringKeyValuePair is class create by myself. You have to implements yours. Basically, just replace the .getKey() with your variable name and .getValue() with value that you want to send over. – Joey Chong Mar 24 '16 at 03:10
  • in the PHP do i use $name = $_POST['name'];? and ill try this! – Lazar Kukolj Mar 24 '16 at 03:19
  • also what do i put for _yourVariableList_? – Lazar Kukolj Mar 24 '16 at 03:20
  • are those two methods your own? because if they are can you update your answer with examples please!! :) – Lazar Kukolj Mar 24 '16 at 03:32
  • Basically it is list of variables that you want to pass over to server when you uploading your video. In your case, i believe is `ProductOwnerEmail` and `ProductKeyCode`. However, I remarked it and hardcode it for your better understanding. – Joey Chong Mar 24 '16 at 06:01
  • thanks for the update! ill take a look at it now but how should the PHP look! – Lazar Kukolj Mar 25 '16 at 22:56
  • Sorry @user6042879, I am not familiar with PHP. Normally when you perform a HTTP post to PHP, how you get a variable? I believe you need to use sometime like `$ProductOwnerEmail = $_POST["ProductOwnerEmail"];` instead and not from `$_FILES`. – Joey Chong Mar 26 '16 at 08:55
  • okay so far you've helped me alot so ill see what i can done here and if this helped me then ill mark it was correct! If i have any other quetions ill ask you – Lazar Kukolj Mar 26 '16 at 23:39
  • I figured out how to do it! its similar to this code but the correct answer is this: http://stackoverflow.com/a/23648537/6042879 – Lazar Kukolj Mar 27 '16 at 14:03
  • 1
    ok. It look same as my answer which need to $_POST instead. Anyways, it is great that you get the answer. – Joey Chong Mar 27 '16 at 15:30
0

U can use Volley lib for this purpose. It also Keep your requests in a queue and easy to use http://developer.android.com/training/volley/index.html

Adeel Turk
  • 897
  • 8
  • 23