从 android 发送特殊字符(字符串)到 php
sending Special chars (String) from android to php
我的问题是,当发送像 éèçà
或 يبلا
这样的字符串时,数据库中插入的值是问号 ???????
这是我的 java 代码
private void uploadFile(String filePath) {
try {
try {
HttpPost httpost = new HttpPost(url_upload);
MultipartEntity entity = new MultipartEntity();
entity.addPart("Mdp", new StringBody(mdp));
entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà"));
entity.addPart("type", new StringBody(type));
if(type.equals("1")) {
entity.addPart("Emailm", new StringBody(email));
entity.addPart("Emailp", new StringBody(emaildest));
}else {
entity.addPart("Emailp", new StringBody(email));
entity.addPart("Emailm", new StringBody(emaildest));
}
if (attach){
entity.addPart("attachement", new FileBody(new File(filePath)));
entity.addPart("attacher", new StringBody("oui"));
}else{
entity.addPart("attacher", new StringBody("non"));
}
httpost.setEntity(entity);
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httpost);
if (response != null) {
HttpEntity entity2 = response.getEntity();
String responseString = EntityUtils.toString(entity2, "UTF-8");
System.out.println(responseString);
json2=new JSONObject(responseString);
} else { //erreur
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
}
}
php代码
<?php
date_default_timezone_set ("Africa/Algiers");
include('connect.php');
$response = array();
if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) && isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) {
$Emailm = $db->real_escape_string($_REQUEST['Emailm']);
$Emailp = $db->real_escape_string($_REQUEST['Emailp']);
$Mdp = $db->real_escape_string($_REQUEST['Mdp']);
$Msg=$db->real_escape_string($_REQUEST['Msg']);
$Mdp = $_REQUEST['Mdp'];
$type= $_REQUEST['type'];
$target_path1 = "attachement/";
if($type=="0"){ //compte patient
if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}else { //compte médecin
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}
$b=false;
$a=false;
if ($row_cnt>0) {
if($_REQUEST['attacher']=="oui"){
$filecount = 0;
$files = glob($target_path1 . "*");
if ($files){
$filecount = count($files);
}
$nomFichier=$filecount.basename( $_FILES['attachement']['name']) . ".a";
$target_path1 = $target_path1 . $nomFichier;
$nomFichierOrigin=basename( $_FILES['attachement']['name']);
if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) {
$b=true;}
}
else{
$nomFichier="";
$nomFichierOrigin="";
};
$h=new DateTime("now") ;
$s=$h->format('G:i:s');
$s2=$h->format('Y-m-d');
if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`)
VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')"))
{$a=true;}
if($a==true && $b==true && $_REQUEST['attacher']=="oui"){
$dernierId = $db->insert_id;
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
if (mysqli_num_rows($result) > 0) {
$response["Msg"] = array();
while ($row = mysqli_fetch_array($result)) {
$Msg = array();
$Msg["cle"] = intval($row["cle"]);
$Msg["heure"] = $row["heure"];
$Msg["date"] = $row["date"];
$Msg["email_m"] = $row["email_m"];
$Msg["email_p"] = $row["email_p"];
$Msg["message"] = $row["message"];
$Msg["type"] = $row["type"];
$Msg["attachement"] = $row["attachement"];
$Msg["attachementNomOrigin"] = $row["attachementNomOrigin"];
array_push($response["Msg"], $Msg);
}
$response["success"] = 1;
$response["message"] = "Succès";
}}} else
if($a==true && $_REQUEST['attacher']=="non"){
$response["success"] = 1;
$response["message"] = "Succès";
}else{
$response["success"] = 0;
$response["message"] = "Erreur lors de l'envoi";
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Email ou Mot de passe incorrect";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Champs manqués";
echo json_encode($response);
}
?>
这里是 connect.php
<?php
$db = new mysqli('localhost', 'root', '', 'rechmed');
mysqli_set_charset($db,'utf8');
?>
非常感谢,我花了几天时间寻找解决这个问题的方法..
在您的 php 代码中,每次尝试检索即将到来的值时尝试使用 utf8_decode($_REQUEST['request_value'])。
经过长时间的搜索,我发现了很多结果……none 其中解决了我的问题。但是从这些结果中,我了解到是什么导致了这个问题。所以对于有同样问题的每个人这样做:
A/ 首先检查你的数据库(我的问题在这里)。
在 phpMyAdmin select utf8_general_ci 中创建数据库时
看截图:http://i62.tinypic.com/2ards7.png
这是我的 table 结构:https://www.dropbox.com/s/d0hgdrbx14cbl85/rechmed.sql?dl=0
B/ 转到 java 源代码并添加:(在我的例子中)
entity.addPart("Msg", new StringBody(URLEncoder.encode("HERE IS THE STRING éçèà"));
而不是:
entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà"));
而不是 posting "your String" post "URLEncoder.encode(msg))"
C/ 现在转到您的 php 代码并解码您的 url 编码字符串
就我而言(在将其插入数据库之前):
现在我用这个:
$Msg = $_REQUEST['Msg'];
$Msg= $db->real_escape_string(htmlentities(urldecode($Msg)));
而不是仅仅使用:
$Msg = $_REQUEST['Msg'];
这是我的 java 代码:
private void uploadFile(String filePath) {
try {
try {
HttpPost httpost = new HttpPost(url_upload);
MultipartEntity entity = new MultipartEntity();
entity.addPart("Mdp", new StringBody(mdp));
entity.addPart("Msg", new StringBody(URLEncoder.encode(msg)));
entity.addPart("type", new StringBody(type));
if(type.equals("1")) {
entity.addPart("Emailm", new StringBody(email));
entity.addPart("Emailp", new StringBody(emaildest));
}else {
entity.addPart("Emailp", new StringBody(email));
entity.addPart("Emailm", new StringBody(emaildest));
}
if (attach){
entity.addPart("attachement", new FileBody(new File(filePath)));
entity.addPart("attacher", new StringBody("oui"));
}else{
entity.addPart("attacher", new StringBody("non"));
}
httpost.setEntity(entity);
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httpost);
if (response != null) {
HttpEntity entity2 = response.getEntity();
String responseString = EntityUtils.toString(entity2, "UTF-8");
System.out.println(responseString);
json2=new JSONObject(responseString);
} else { //erreur
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
}
}
这是 php 代码:
<?php
date_default_timezone_set ("Africa/Algiers");
include('connect.php');
$response = array();
if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) && isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) {
$Emailm = $db->real_escape_string($_REQUEST['Emailm']);
$Emailp = $db->real_escape_string($_REQUEST['Emailp']);
$Mdp = $db->real_escape_string($_REQUEST['Mdp']);
$Msg = $_REQUEST['Msg'];
$Mdp = $_REQUEST['Mdp'];
$Msg= $db->real_escape_string(htmlentities(urldecode($Msg)));
$type= $_REQUEST['type'];
$target_path1 = "attachement/";
if($type=="0"){ //compte patient
if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}else { //compte médecin
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}
$b=false;
$a=false;
if ($row_cnt>0) {
if($_REQUEST['attacher']=="oui"){
$filecount = 0;
$files = glob($target_path1 . "*");
if ($files){
$filecount = count($files);
}
$nomFichier=$filecount.basename( $_FILES['attachement']['name']) . ".a";
$target_path1 = $target_path1 . $nomFichier;
$nomFichierOrigin=basename( $_FILES['attachement']['name']);
if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) {
$b=true;}
}
else{
$nomFichier="";
$nomFichierOrigin="";
};
$h=new DateTime("now") ;
$s=$h->format('G:i:s');
$s2=$h->format('Y-m-d');
if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`)
VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')"))
{$a=true;}
if($a==true && $b==true && $_REQUEST['attacher']=="oui"){
$dernierId = $db->insert_id;
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
if (mysqli_num_rows($result) > 0) {
$response["Msg"] = array();
while ($row = mysqli_fetch_array($result)) {
$Msg = array();
$Msg["cle"] = intval($row["cle"]);
$Msg["heure"] = $row["heure"];
$Msg["date"] = $row["date"];
$Msg["email_m"] = $row["email_m"];
$Msg["email_p"] = $row["email_p"];
$Msg["message"] = $row["message"];
$Msg["type"] = $row["type"];
$Msg["attachement"] = $row["attachement"];
$Msg["attachementNomOrigin"] = $row["attachementNomOrigin"];
array_push($response["Msg"], $Msg);
}
$response["success"] = 1;
$response["message"] = "Succès";
}}} else
if($a==true && $_REQUEST['attacher']=="non"){
$response["success"] = 1;
$response["message"] = "Succès";
}else{
$response["success"] = 0;
$response["message"] = "Erreur lors de l'envoi";
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Email ou Mot de passe incorrect";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Champs manqués";
echo json_encode($response);
}
?>
这里是允许连接到数据库的 connect.php:
<?php
$db = new mysqli('localhost', 'root', '', 'rechmed');
mysqli_set_charset($db,'utf8');
mysqli_query($db,"SET NAMES 'utf8'");
mysqli_query($db,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
?>
我的问题是,当发送像 éèçà
或 يبلا
???????
这是我的 java 代码
private void uploadFile(String filePath) {
try {
try {
HttpPost httpost = new HttpPost(url_upload);
MultipartEntity entity = new MultipartEntity();
entity.addPart("Mdp", new StringBody(mdp));
entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà"));
entity.addPart("type", new StringBody(type));
if(type.equals("1")) {
entity.addPart("Emailm", new StringBody(email));
entity.addPart("Emailp", new StringBody(emaildest));
}else {
entity.addPart("Emailp", new StringBody(email));
entity.addPart("Emailm", new StringBody(emaildest));
}
if (attach){
entity.addPart("attachement", new FileBody(new File(filePath)));
entity.addPart("attacher", new StringBody("oui"));
}else{
entity.addPart("attacher", new StringBody("non"));
}
httpost.setEntity(entity);
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httpost);
if (response != null) {
HttpEntity entity2 = response.getEntity();
String responseString = EntityUtils.toString(entity2, "UTF-8");
System.out.println(responseString);
json2=new JSONObject(responseString);
} else { //erreur
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
}
}
php代码
<?php
date_default_timezone_set ("Africa/Algiers");
include('connect.php');
$response = array();
if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) && isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) {
$Emailm = $db->real_escape_string($_REQUEST['Emailm']);
$Emailp = $db->real_escape_string($_REQUEST['Emailp']);
$Mdp = $db->real_escape_string($_REQUEST['Mdp']);
$Msg=$db->real_escape_string($_REQUEST['Msg']);
$Mdp = $_REQUEST['Mdp'];
$type= $_REQUEST['type'];
$target_path1 = "attachement/";
if($type=="0"){ //compte patient
if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}else { //compte médecin
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}
$b=false;
$a=false;
if ($row_cnt>0) {
if($_REQUEST['attacher']=="oui"){
$filecount = 0;
$files = glob($target_path1 . "*");
if ($files){
$filecount = count($files);
}
$nomFichier=$filecount.basename( $_FILES['attachement']['name']) . ".a";
$target_path1 = $target_path1 . $nomFichier;
$nomFichierOrigin=basename( $_FILES['attachement']['name']);
if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) {
$b=true;}
}
else{
$nomFichier="";
$nomFichierOrigin="";
};
$h=new DateTime("now") ;
$s=$h->format('G:i:s');
$s2=$h->format('Y-m-d');
if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`)
VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')"))
{$a=true;}
if($a==true && $b==true && $_REQUEST['attacher']=="oui"){
$dernierId = $db->insert_id;
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
if (mysqli_num_rows($result) > 0) {
$response["Msg"] = array();
while ($row = mysqli_fetch_array($result)) {
$Msg = array();
$Msg["cle"] = intval($row["cle"]);
$Msg["heure"] = $row["heure"];
$Msg["date"] = $row["date"];
$Msg["email_m"] = $row["email_m"];
$Msg["email_p"] = $row["email_p"];
$Msg["message"] = $row["message"];
$Msg["type"] = $row["type"];
$Msg["attachement"] = $row["attachement"];
$Msg["attachementNomOrigin"] = $row["attachementNomOrigin"];
array_push($response["Msg"], $Msg);
}
$response["success"] = 1;
$response["message"] = "Succès";
}}} else
if($a==true && $_REQUEST['attacher']=="non"){
$response["success"] = 1;
$response["message"] = "Succès";
}else{
$response["success"] = 0;
$response["message"] = "Erreur lors de l'envoi";
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Email ou Mot de passe incorrect";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Champs manqués";
echo json_encode($response);
}
?>
这里是 connect.php
<?php
$db = new mysqli('localhost', 'root', '', 'rechmed');
mysqli_set_charset($db,'utf8');
?>
非常感谢,我花了几天时间寻找解决这个问题的方法..
在您的 php 代码中,每次尝试检索即将到来的值时尝试使用 utf8_decode($_REQUEST['request_value'])。
经过长时间的搜索,我发现了很多结果……none 其中解决了我的问题。但是从这些结果中,我了解到是什么导致了这个问题。所以对于有同样问题的每个人这样做:
A/ 首先检查你的数据库(我的问题在这里)。 在 phpMyAdmin select utf8_general_ci 中创建数据库时 看截图:http://i62.tinypic.com/2ards7.png 这是我的 table 结构:https://www.dropbox.com/s/d0hgdrbx14cbl85/rechmed.sql?dl=0
B/ 转到 java 源代码并添加:(在我的例子中)
entity.addPart("Msg", new StringBody(URLEncoder.encode("HERE IS THE STRING éçèà"));
而不是:
entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà"));
而不是 posting "your String" post "URLEncoder.encode(msg))"
C/ 现在转到您的 php 代码并解码您的 url 编码字符串 就我而言(在将其插入数据库之前): 现在我用这个:
$Msg = $_REQUEST['Msg'];
$Msg= $db->real_escape_string(htmlentities(urldecode($Msg)));
而不是仅仅使用:
$Msg = $_REQUEST['Msg'];
这是我的 java 代码:
private void uploadFile(String filePath) {
try {
try {
HttpPost httpost = new HttpPost(url_upload);
MultipartEntity entity = new MultipartEntity();
entity.addPart("Mdp", new StringBody(mdp));
entity.addPart("Msg", new StringBody(URLEncoder.encode(msg)));
entity.addPart("type", new StringBody(type));
if(type.equals("1")) {
entity.addPart("Emailm", new StringBody(email));
entity.addPart("Emailp", new StringBody(emaildest));
}else {
entity.addPart("Emailp", new StringBody(email));
entity.addPart("Emailm", new StringBody(emaildest));
}
if (attach){
entity.addPart("attachement", new FileBody(new File(filePath)));
entity.addPart("attacher", new StringBody("oui"));
}else{
entity.addPart("attacher", new StringBody("non"));
}
httpost.setEntity(entity);
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httpost);
if (response != null) {
HttpEntity entity2 = response.getEntity();
String responseString = EntityUtils.toString(entity2, "UTF-8");
System.out.println(responseString);
json2=new JSONObject(responseString);
} else { //erreur
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
}
}
这是 php 代码:
<?php
date_default_timezone_set ("Africa/Algiers");
include('connect.php');
$response = array();
if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) && isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) {
$Emailm = $db->real_escape_string($_REQUEST['Emailm']);
$Emailp = $db->real_escape_string($_REQUEST['Emailp']);
$Mdp = $db->real_escape_string($_REQUEST['Mdp']);
$Msg = $_REQUEST['Msg'];
$Mdp = $_REQUEST['Mdp'];
$Msg= $db->real_escape_string(htmlentities(urldecode($Msg)));
$type= $_REQUEST['type'];
$target_path1 = "attachement/";
if($type=="0"){ //compte patient
if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}else { //compte médecin
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}
$b=false;
$a=false;
if ($row_cnt>0) {
if($_REQUEST['attacher']=="oui"){
$filecount = 0;
$files = glob($target_path1 . "*");
if ($files){
$filecount = count($files);
}
$nomFichier=$filecount.basename( $_FILES['attachement']['name']) . ".a";
$target_path1 = $target_path1 . $nomFichier;
$nomFichierOrigin=basename( $_FILES['attachement']['name']);
if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) {
$b=true;}
}
else{
$nomFichier="";
$nomFichierOrigin="";
};
$h=new DateTime("now") ;
$s=$h->format('G:i:s');
$s2=$h->format('Y-m-d');
if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`)
VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')"))
{$a=true;}
if($a==true && $b==true && $_REQUEST['attacher']=="oui"){
$dernierId = $db->insert_id;
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
if (mysqli_num_rows($result) > 0) {
$response["Msg"] = array();
while ($row = mysqli_fetch_array($result)) {
$Msg = array();
$Msg["cle"] = intval($row["cle"]);
$Msg["heure"] = $row["heure"];
$Msg["date"] = $row["date"];
$Msg["email_m"] = $row["email_m"];
$Msg["email_p"] = $row["email_p"];
$Msg["message"] = $row["message"];
$Msg["type"] = $row["type"];
$Msg["attachement"] = $row["attachement"];
$Msg["attachementNomOrigin"] = $row["attachementNomOrigin"];
array_push($response["Msg"], $Msg);
}
$response["success"] = 1;
$response["message"] = "Succès";
}}} else
if($a==true && $_REQUEST['attacher']=="non"){
$response["success"] = 1;
$response["message"] = "Succès";
}else{
$response["success"] = 0;
$response["message"] = "Erreur lors de l'envoi";
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Email ou Mot de passe incorrect";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Champs manqués";
echo json_encode($response);
}
?>
这里是允许连接到数据库的 connect.php:
<?php
$db = new mysqli('localhost', 'root', '', 'rechmed');
mysqli_set_charset($db,'utf8');
mysqli_query($db,"SET NAMES 'utf8'");
mysqli_query($db,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
?>