当我尝试 post 带有从 Android 到 Php 的斯堪的纳维亚字符的字符串时,如 ÅÄÖ

What is wrong when i try to post String with scandinavian characters from Android to Php, like ÅÄÖ

我找不到任何原因说明我传递给 php 的斯堪的纳维亚字符没有正确显示和存储。

当我使用我的网络应用程序时,斯堪的纳维亚字符与 php 和数据库一起正常工作。

如果我将消息从 webapp 写入数据库并将其加载到 android 应用程序,字符会正确显示。

在android

String message = "Tässä"

try {

    // open a connection to the site
    URL url = new URL("own_url");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();


    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "");
    con.setRequestProperty("Accept-Language", "fi, en-US,fi;q=0.6, en;q=0.5");
    con.setRequestProperty("Accept-Charset", "UTF-8");

    // Send post request
    con.setDoOutput(true);

    String urlParameters = "message=" + message;

在PHP

$message = $_POST['message'];
error_log($message);

error_log 显示为 T\xe4ss\xe4

mysqli_set_charset($con, 'utf8');
date_default_timezone_set('UTC');

$sql = "INSERT INTO messages SET message='$message'";
mysqli_query($con, $sql);

在数据库中是 T?ss

编辑

更改查询生成以防止 SQL 注入,感谢 user2864740 的建议。

$stmt = $con->prepare("INSERT INTO messages (message) VALUES (?)");
$stmt->bind_param("s", $message);
$stmt->execute();

我找到了我的问题的解决方案,我需要像这样将 urlParameters 设为 byte[]。

String urlParameters = "message=" + message;
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

DataOutputStream wr = new DataOutputStream( con.getOutputStream()){};
wr.write( postData );

但是 API 小于 19 有问题,有这个:

StandardCharsets.UTF_8

字段要求 API 级别 19(当前最低为 14):java.nio.charset.StandardCharsets#UTF_8