Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plus symbol changes to space when sending data to a php acript and saving in mysql table through android

I am trying to send an arraylist converted to string and then urlencoded to a php script through a POST requestwhich will save that data in a mysql table. Everything is fine except the '+' symbol in the database are replaced by ' '(space).

This is the javacode in android that i am using to send POST request

ContactNumbers is an ArrayList containg phonemunbers

ContactNumbers.toString() is [+919401557473, +919085425753, +919435448667, +9954263031]

            param = "param="+ContactNumbers.toString(),;

            try {
                String yourURL = "http://54.169.88.65/events/eventmain/get_users2.php";
                URL url = new URL(yourURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setFixedLengthStreamingMode(param.getBytes().length);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                OutputStream out = new BufferedOutputStream(connection.getOutputStream());
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
                writer.write(param);
                writer.flush();
                writer.close();
                out.close();
                connection.connect();

The php script get_users2.php that i am using is as follows:

<?php

$r = $_POST['param'];
mysql_connect("localhost","root","magento");
mysql_select_db("Eventmain");
$sql = "INSERT INTO `data`(`data`) VALUES ('$r')";
mysql_query($sql);

?> 

The data table in mysql is as follows:

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

The column data has Collation equal to utf8_general_ci.

Please Help.

like image 426
Vipin Kumar Avatar asked Dec 03 '25 18:12

Vipin Kumar


1 Answers

The fact is that you are URLEncoding params on Android side, and then URLDecoding them with your PHP script.

Unfortunately, the URL decoding of a '+' is a whitespace.

You can try to change your '+' character on your Android application with another character that is not affected by URL Encoding/Decoding, and then on PHP side you can substitute that character with '+' after URL decoding.

like image 54
javatutorial Avatar answered Dec 06 '25 08:12

javatutorial