i am new android developer who developing a persian real estate android project.i have a form which inserts new estate to mysqli table using php file.but when i use persian characters in every field of that form it actually inserts null values to every fields of table.but when i use english characters it inserts to the table completely! for example here is my english characters json sent from android :
{"price":"5668845555","address":"ssfghhvcxdd","phoneNumber":"544585533","bedRoom":6,"describtion":"fdstghgccff","isForSale":"rent","isCommertial":"not commertial","meter":545,"image":"android.graphics.Bitmap@c10755a"}
and this is included persian characters json sent from android:
{"price":"5668845555","address":"بلاتذزییبغاتتاا","phoneNumber":"544585533","bedRoom":6,"describtion":"یباننترلبقثس","isForSale":"rent","isCommertial":"not commertial","meter":545,"image":"android.graphics.Bitmap@c10755a"}
while both json's is completely correct the json with persian characters dont inserts to table correctly!
this is my php file:
<?php
define("SERVER_NAME", "localhost");
define("USER_NAME", "root");
define("PASSWORD", "");
define("DATABASE_NAME", "mydb");
$conn = mysqli_connect(SERVER_NAME, USER_NAME, PASSWORD, DATABASE_NAME);
header("content-type: application/json;charset=utf-8");
$jsonContent = file_get_contents('php://input');
mysqli_set_charset($conn, "utf8");
utf8_encode($jsonContent);
$arrayContent = json_decode($jsonContent,true);
$price = $arrayContent["price"];
$address = $arrayContent["address"];
$phoneNumber=$arrayContent["phoneNumber"];
$image=$arrayContent["image"];
$meter=$arrayContent["meter"];
$bedRoom=$arrayContent["bedRoom"];
$isCommertial=$arrayContent["isCommertial"];
$isForSale=$arrayContent["isForSale"];
$describtion=$arrayContent["describtion"];
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO estates (price,address,phoneNumber,image,meter,bedRoom,isCommertial,isForSale,describtion)
VALUES ('$price','$address','$phoneNumber','$image','$meter','$bedRoom','$isCommertial','$isForSale','$describtion')";
if ($conn->query($sql) === TRUE) {
echo "Estate added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
and this is what mysql table looks like after these two json's
and i have to say that all of my fields collation is utf8_persian_ci
so what do you think is my mistake? thank you
Problem Solved! by using String Formatter Class in android i sent json in right way to php!
public class StringFormatter {
// convert UTF-8 to internal Java String format
public static String convertUTF8ToString(String s) {
String out = null;
try {
out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
// convert internal Java String format to UTF-8
public static String convertStringToUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
}