如何通过Http POST请求发送表情符号

时间:2018-05-01 05:44:49

标签: android mysql http-post

我有一个应用程序使用http请求将数据发送到MySQL服务器,但是 在服务器上显示“????”而不是表情符号。 当我在服务器中插入数据手册时表情符号没问题。 (Collat​​ion = utf8mb4_general_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'];

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

2 个答案:

答案 0 :(得分:1)

我希望这对你有用。

更改服务器连接排序规则,如下所示

Collation = utf8mb4_unicode_ci

然后发送它而不进行编码。

答案 1 :(得分:0)

在php代码中,我应该使用utf8mb4作为charset:

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

一切都是绝对正确的