如何发送带有httppost请求的emoji

r6vfmomb  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(513)

我有一个应用程序,它通过http请求将数据发送到mysql服务器,但在服务器上显示“???”而不是emoji。当我在服务器中插入数据手册时,emoji没有问题(排序规则=utf8mb4\u常规\u ci)
我在android中发送数据的代码:

public static String sendData(String Address, HashMap hashMap)
{
    try
    {
        Object[] keys = hashMap.keySet().toArray();
        Object[] values = hashMap.values().toArray();

        String data = Utils.Encoder(keys[0].toString(), values[0].toString());
        for (int i = 1; i < hashMap.size(); i++)
        {
            data+= "&" + Utils.Encoder(keys[i].toString(), values[i].toString());
        }

        URL url = new URL(Address);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
        connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
        connection.setDoOutput(true);
        DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
        dStream.writeBytes(data);
        dStream.flush();
        dStream.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = "";
        StringBuilder responseOutput = new StringBuilder();

        while ((line = br.readLine()) != null)
        {
            responseOutput.append(line);
        }
        br.close();

        return responseOutput.toString();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return null;
}

public static String getData(String Address)
{
    URL url = null;
    try
    {
        url = new URL(Address);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
        connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");

        InputStream in = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line = "";
        StringBuilder responseOutput = new StringBuilder();
        while ((line = br.readLine()) != null)
        {
            responseOutput.append(line);
        }
        br.close();
        return responseOutput.toString();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return "";
}

public static String Encoder(String key, String value)
{

    try
    {
        return URLEncoder.encode(base64Key, "UTF-8") + "=" + URLEncoder.encode(base64Value, "UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }

    return "";
}
}

我认为我的编码器有问题,所以我使用base64方法而不是utf-8:

public static String Encoder(String key, String value)
{

    byte[] valueBytes =  value.getBytes();
    byte[] keyBytes =  key.getBytes();
    return Base64.encodeToString(valueBytes,Base64.DEFAULT) + "=" + Base64.encodeToString(keyBytes,Base64.DEFAULT);

    }

但当我使用它时,不会在服务器中显示任何内容。
这是我的php代码:(不是所有的代码)

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8mb4');

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if (!$conn->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
$conn->character_set_name();
}

$alias=$_POST['Alias'];
$text=$_POST['Text'];

编辑:我想发送波斯字符的数据

bihw5rsg

bihw5rsg1#

在php代码中我应该使用 utf8mb4 对于字符集:

if (!$conn->set_charset("utf8mb4")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    $conn->character_set_name();
}

一切都绝对正确

jobtbby3

jobtbby32#

我希望这对你有用。
按如下所示更改服务器连接排序规则

Collation = utf8mb4_unicode_ci

然后不编码发送。

相关问题