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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With