我正在尝试发送一个转换为字符串的数组列表,然后通过 POST 请求将 urlencode 编码为 php 脚本,这会将数据保存在 mysql 表中。一切正常,除了数据库中的“+”符号被“”(空格)替换。
这是我用来发送 POST 请求的 android 中的 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中的data
表如下:
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 ;
列 data
的 Collation
等于 utf8_general_ci
。
请帮助。
最佳答案
事实是您在 Android 端对参数进行 URLEncoding,然后使用 PHP 脚本对它们进行 URLDecoding。
不幸的是,“+”的 URL 解码是一个空格。
您可以尝试用另一个不受 URL 编码/解码影响的字符更改 Android 应用程序中的“+”字符,然后在 PHP 端,您可以在 URL 解码后用“+”替换该字符。
关于java - 将数据发送到 php 脚本并通过 android 保存在 mysql 表中时,加号变为空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964676/