0

I am currently using the HTTPURLConnection class in Android to send some images to my php server. The image transfer runs fine, but the main problem is that I need to send a parameter too, (like a number, for example: 2). And I don't really know how to do it. And when I transfer the parameter too, how can I obtain it and use it in my php server?

This is my code:

        URL url = new URL(url_path);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setChunkedStreamingMode(1024);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data");

        OutputStream outputStream = conn.getOutputStream();
        InputStream inputStream = c.getContentResolver().openInputStream(path);

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

        int bytesRead;
        while ((bytesRead = inputStream.read(buffer, 0, bufferSize)) != -1) {
            outputStream.write(buffer, 0, bytesRead); //Sending the image
            //Here is where I need the parameter to be sent too.

        }
        outputStream.flush();
        inputStream.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            Log.i("result", line);
        }
        reader.close();
        conn.disconnect();

And this is my php code, is pretty simple:

<?php
    $file_name = date("U").".jpg"; //I'm going to use the parameter here
    $server_path = "uploads/";
    $web_path = "http://xxxxxxxxxxxxxxxxxxxxxxx/";
    $file = $server_path.$file_name;
    file_put_contents($file,"");


    $fp = fopen("php://input", 'r');
    while ($buffer =  fread($fp, 8192)) {
        file_put_contents($file,$buffer,FILE_APPEND | LOCK_EX);
    }
    echo $web_path.$file_name;
   ?>

Thank you very much!

  • You can find the answers here: [Add and Send Parameter to PHP Server in Android](https://stackoverflow.com/questions/32643944/sending-variable-to-php-server-from-android) [Add and Send Parameter to PHP Server in Android - 2](https://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post) I hope that helps! – Shreya Mar 16 '18 at 04:51
  • Thanks! But it didn't work, sadly. – Arturo Aguilera Mar 16 '18 at 05:32

0 Answers0