2

I have a problem with the parameters passed to an URL by POST. When in PHP i try to retrieve them it says that $_POST is empty:

<?php
require_once 'credentials.php';
$connection = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE);
$query = 'INSERT INTO ratings (disco, rating) VALUES (' .mysqli_real_escape_string($connection, $_POST["disco"]). ', ' . mysqli_real_escape_string($connection, $_POST["rating"]) . ')';
$result = mysqli_query($connection, $query);
echo json_encode($result);
mysqli_close($connection);  
?>

And my Android code looks like this:

@Override
            protected String doInBackground(String... params) {
                String response = "";
                try {
                    URL url = new URL(params[0]);
                    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                    urlConnection.setConnectTimeout(10000);
                    urlConnection.setReadTimeout(10000);
                    urlConnection.setDoOutput(true);
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setDoOutput(true);
                    urlConnection.setDoInput(true);
                    urlConnection.connect();

                    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
                    parameters.add(new BasicNameValuePair("disco", params[1]));
                    parameters.add(new BasicNameValuePair("rating", params[2]));

                    OutputStream outputStream = urlConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                    writer.write(getQuery(parameters));
                    writer.flush();
                    writer.close();
                    outputStream.close();
                    int responseCode = urlConnection.getResponseCode();
                    if(responseCode == HttpURLConnection.HTTP_OK) {
                        String line;
                        BufferedReader br=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                        while((line = br.readLine()) != null) {
                            response += line;
                        }
                    }
                } catch(Exception e) {
                    System.out.println(e.getMessage());
                }
                return response;
            }

            private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
                StringBuilder result = new StringBuilder();
                boolean first = true;

                for(NameValuePair pair : params) {
                    if(first)
                        first = false;
                    else
                        result.append("&");

                    result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
                    result.append("=");
                    result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
                }

                return result.toString();
            }

What's wrong with this? Thanks in advance

Edit: updated code and still doesn't work.

Xabi
  • 159
  • 1
  • 3
  • 13
  • See this http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post – Rohit5k2 Feb 05 '16 at 20:48
  • you logcat print please.. – Amine ABDALKHALKI Feb 05 '16 at 20:49
  • @Rohit5k2 still the same, tried that before – Xabi Feb 05 '16 at 21:15
  • Amine Leo, it doesn't show any error neither warning – Xabi Feb 05 '16 at 21:17
  • Is that all your code? Because it doesn't look like you're ever reading the response from the server in your Android's doInBackground method – Cruceo Feb 05 '16 at 23:00
  • before OutputStream try to add this: urlConnection.connect(); and also after writer.close() add int response = urlConnection.getResponseCode(); and check if the response is 200 – 633kM Feb 06 '16 at 00:28
  • I've put the urlConnection.getResponseCode(); and it's giving code 200, the problem is in the $_POST on PHP as it's getting null values. I receive "false" from echo json_encode($result); and if i change it to echo json_encode($_POST['disco']); it returns null. – Xabi Feb 06 '16 at 08:44
  • Remove all code from your php script. Only put in `var_dump($_POST); var_dump(REQUEST);` Then tell what you get as response. So you shoud first add some code to android code to read the response text. The echos. – greenapps Feb 06 '16 at 09:36
  • greenapps if I only put those 2 echo it shows array(0) {} in both of them, so they're comming empty and I don't know why – Xabi Feb 06 '16 at 09:47
  • `writer.write(getQuery(parameters));`. Have you ever checked what getQuery() produces? Try with `writer.write(getQuery(parameters).getBytes());`. – greenapps Feb 07 '16 at 08:30
  • You are furst constructing basic name value pairs. And then throw them away for a string made by a string builder. Too much steps. – greenapps Feb 07 '16 at 08:32
  • I found out that the query is working correctly, it gives the needed parameters. The problem is that the PHP is not receiving the $_POST neither the $_REQUEST. Have tried using Chrome's Rest Api caller application, and found out that $_POST and $_REQUEST are empty. – Xabi Feb 07 '16 at 10:28
  • Add a content description form url encioded. – greenapps Feb 08 '16 at 06:09

1 Answers1

0
"&disco="

Make that:

"disco="
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • It's the same, even using the link provided by Rohit5k2, it just says that $_POST is an empty array if i send it as php response with echo json_encode($_POST); – Xabi Feb 06 '16 at 09:24
  • You could make your php code better by first checking if the post variables are coming in correctly. Use isset(). Echo appropriate messages if not. If all is ok then follows the database code. Thats how you should code. – greenapps Feb 06 '16 at 09:32