将数据发送到 php 脚本并保存到 mysql table 到 android 时,加号变为 space

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

我正在尝试发送一个转换为字符串的数组列表,然后通过 POST 请求将 urlencode 编码为 php 脚本,这会将数据保存在 mysql table 中.一切都很好,除了数据库中的“+”符号被替换为“”(space)。

这是 android 中我用来发送 POST 请求的 javacode

ContactNumbers 是一个包含音素的 ArrayList

ContactNumbers.toString()[+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();

我使用的php脚本get_users2.php如下:

<?php

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

?> 

mysql中的datatable如下:

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 ;

dataCollation 等于 utf8_general_ci

请帮忙。

事实是您在 Android 端 URL 编码参数,然后 URL 使用您的 PHP 脚本对其进行解码。

不幸的是,“+”的 URL 解码是一个空格。

您可以尝试将 Android 应用程序中的“+”字符更改为不受 URL Encoding/Decoding 影响的另一个字符,然后在 PHP在 URL 解码后,您可以用“+”替换该字符。