2

My project is to upload the image, audio files with some parameter(like description and date).

Though Google announced to use HttpURLConnection instead of httpclient. I am using HttpURLConnection.

I have an code which upload the image and audio in server folder.

But the description which I send is not received in the server.

Like this question many in Stackover flow. But I did not get exact solution.

My android code is:

                FileInputStream fileInputStream = new FileInputStream(sourceFile_image);
                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", fileName);

                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);

                //adding parameter 

                String description = ""+"Desceiption about the image";

                // Send parameter #name
                dos.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
                dos.writeBytes("Content-Length: " + description.length() + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(description + lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);

                // Send #image

                dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + 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 > 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);

And Php Code:

$description= $_POST['description'];

   $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
   if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
       echo "success";
   } else{
       echo "fail";
   }

Image and audio updating successfully.

But parameter not received or I dont know how to receive the parameter in php.

Is my android and php code to send and receive parameter is correct?

Is Any other solution.

I am trying lot but not works and not getting idea too.

John
  • 45
  • 1
  • 6

2 Answers2

0
// Send parameter #name
dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);

That should be:

// Send parameter #description
dos.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd);
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • ya modified but not working sir.Is parameter added in android code is correct? Image and audio storing ? But cannot get parameter. – John Oct 29 '14 at 17:31
0

check this link

Android code:

String description = ""+"Desceiption about the image";


            dos.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd); 
            //dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
            //dos.writeBytes("Content-Length: " + description.length() + lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes(description); // mobile_no is String variable
            dos.writeBytes(lineEnd);

Php code:

$description =$_POST['description'];
Community
  • 1
  • 1
Amir
  • 1,066
  • 1
  • 13
  • 26
  • This still does'nt work. File is still uploaded but not the parameter – RicNjesh Apr 10 '15 at 19:15
  • RicNjesh @ http://stackoverflow.com/questions/26686806/httpurlconnection-to-send-image-audio-and-video-files-with-parameter-may-stri – Amir Apr 12 '15 at 09:58