0

I am trying to upload two images along with some parameters to a server through my android application. After having searched online and following the instructions from here and here, as well as other sources, I have the following code:

String boundary = "***" + System.currentTimeMillis() + "***";
String twoHyphens = "--";
String crlf = "\r\n";
String output = "";
try {
            HttpURLConnection httpUrlConnection = null;
            URL url = new URL(myUrl);
            httpUrlConnection = (HttpURLConnection) url.openConnection();
            httpUrlConnection.setUseCaches(false);
            httpUrlConnection.setDoInput(true);
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");

            httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
            httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
            httpUrlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
            httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);


            DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
            request.writeBytes(twoHyphens + boundary + crlf);

            // Convert and add first image
            ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
            params[0].compress(Bitmap.CompressFormat.JPEG, 100, bao1);
            byte[] ba1 = bao1.toByteArray();


            request.writeBytes("Content-Disposition: form-data; name=\"image1\";filename=\"image1\"" + crlf);
            request.writeBytes(crlf);
            request.write(ba1);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + crlf);

            // Convert and add second image
            ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
            params[1].compress(Bitmap.CompressFormat.JPEG, 100, bao2);
            byte[] ba2 = bao2.toByteArray();

            request.writeBytes("Content-Disposition: form-data; name=\"image2\";filename=\"image2\"" + crlf);
            request.writeBytes(crlf);
            request.write(ba2);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + crlf);

            request.writeBytes("Content-Disposition: form-data; name=\"username\"" + crlf);
            request.writeBytes(crlf);
            request.writeBytes(username);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + twoHyphens);

            request.writeBytes("Content-Disposition: form-data; name=\"datestr\"" + crlf);
            request.writeBytes(crlf);
            request.writeBytes(timeStampString);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + twoHyphens);

            request.flush();
            request.close();

            int responseCode = httpUrlConnection.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
                BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream, Charset.forName("UTF-8")));

                String line;
                while ((line = responseStreamReader.readLine()) != null) {
                    output = line;
                    Log.d(TAG, line);
                }
                responseStreamReader.close();
            }
            httpUrlConnection.disconnect();

            if (output == "") {
                httpResultsReturned = false;
            } else {
                httpResultsReturned = true;
            }

        } catch (ProtocolException e) {
            e.printStackTrace();
            return "failed";
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "failed";
        } catch (IOException e) {
            e.printStackTrace();
            return "failed";
        }

On the server side I try to access the data as follows:

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $image1 = $_FILES['image1']['name'];
    $image2 = $_FILES['image2']['name'];
    $datestr= $_POST['datestr'];
    $username= $_POST['username'];
}

?>

Eventually, both images are successfully transmitted, however I cannot send/receive the extra parameters. I receive the responses correctly, but throughout all the php code (there are parts that I omitted in this question), it seems like none of the parameters are sent/received.

In this question, AndroSco shared the solution that worked for him, but in his php file it looks like he only accesses the image and not the parameter...

Since I don't have a lot of experience on this field, I believe that there is probably something very obvious that I do incorrectly/don't do at all!

Any suggestions will be appreciated!

Thanks!

thomai
  • 1
  • 2
  • Compress your bitmaps directly to the dataoutputstream. You do not need an intermediate bytearrayoutputstream. – greenapps Jun 29 '18 at 17:39
  • Hi! Thanks for the suggestion! I'll do it! Though it doesn't seem that this can be affecting my problem! – thomai Jul 02 '18 at 08:01

1 Answers1

0

After much frustration, I found the bug in my code. After having imported the two images in the transmitted message and when I wanted to import the other parameters, I wrote the boundary incorrectly. Instead of adding this:

request.writeBytes(twoHyphens + boundary + crlf);

having a new line at the end, I wrote this:

request.writeBytes(twoHyphens + boundary + twoHyphens);

adding two hyphens at the end of the line.

After replacing the twoHyphens with crlf, everything worked nicely!

thomai
  • 1
  • 2