Reputation: 612
I have an application which send data to MySQL server with http request, But in the server show "????" instead of emoji. When I insert data manual in the server no problem with emoji. (Collation = utf8mb4_general_ci)
My code to send data in 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 "";
}
}
I think my encoder have problem, so i use Base64 method instead of 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);
}
But when i use this, does not to show anything in server.
and this is my php code:(it is not all code)
$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'];
Edited: I want to send data with Persian character
Upvotes: 2
Views: 3041
Reputation: 612
In php code I should use utf8mb4
for charset:
if (!$conn->set_charset("utf8mb4")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
$conn->character_set_name();
}
Everything is absolutely correct
Upvotes: 1
Reputation: 1219
I hope this will work for you.
Change Server connection collation as below
Collation = utf8mb4_unicode_ci
Then send it without encode.
Upvotes: 1