1

As i took the code from the one of stack overflow answers. Send .txt file, document file to the server in android

Modifying the following code, I'm trying to POST audio/video file to online php server. Everything works fine the file is uploading correctly and saving in database but the problem is I am also sending parameters with the file so that i can recognize which user is sending recordings, (user_id and candidate_id and file_type) but in database their values are null/0.

Please someone tell me whats wrong with my code why its not working for params. As i checked my php side server with POSTMAN all the values are saving correctly with file.

try {
            FileInputStream fileInputStream = new FileInputStream(
                    sourceFile);
            URL url = new URL(Config.URL_UPLOAD);
            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("uploadedfile", fileName);

            dos = new DataOutputStream(conn.getOutputStream());
            //here am adding the params

            param.put("user_id", Constants.user_id);
            param.put("candidate_id", "candiidateid1");
            param.put("file_type", Constants.file_type);
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(dos, "UTF-8"));
            writer.write(getPostDataString(param));
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";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);

            dialog.dismiss();
            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "
                    + serverResponseMessage + ": " + serverResponseCode);
            DataInputStream inStream = null;
            if (serverResponseCode == 200) {
                try {
                    Log.e("isStream = ", "" + inStream);
                    inStream = new DataInputStream(conn.getInputStream());
                    String str;
                    Log.e("isStream = ", "" + inStream);
                    while ((str = inStream.readLine()) != null) {
                        Log.e("Debug", "Server Response " + str);
                    }
                    inStream.close();
                    Log.e("isStream = ", "" + inStream);
                } catch (IOException ioex) {
                    Log.e("Debug", "error: " + ioex.getMessage(), ioex);
                }

            }

            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss();
            ex.printStackTrace();


            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            dialog.dismiss();
            e.printStackTrace();


            Log.e("Upload file to server ",
                    "Exception : " + e.getMessage(), e);
        }
Community
  • 1
  • 1
Shehroz Saleem
  • 196
  • 2
  • 15
  • DataOutputStream is a Java utility class to encode simple Java values. PHP probably cannot handle that encoding natively, so you should look for other, cross-language ways to transfer meta data. – JimmyB Aug 16 '16 at 19:02
  • Have a look at https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#addRequestProperty(java.lang.String,%20java.lang.String) for instance. – JimmyB Aug 16 '16 at 19:07

1 Answers1

0

Refer the below URL it ha clear answer to pass the parameters

Sending files using POST with HttpURLConnection

Community
  • 1
  • 1
gowtham
  • 26
  • 2