从android向php发送特殊字符(字符串)

从android向php发送特殊字符(字符串),php,android,post,special-characters,Php,Android,Post,Special Characters,我的问题是,在发送字符串(如èèèèa或يبا 这是我的java代码 private void uploadFile(String filePath) { try { try { HttpPost httpost = new HttpPost(url_upload); MultipartEntity entity = new MultipartEntity(); entity.addPar

我的问题是,在发送字符串(如
èèèèa
يبا

这是我的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);
      }
      ?>

在php代码中,每次尝试检索即将到来的值时,请尝试使用utf8_解码($_REQUEST['REQUEST_value'])。

经过长时间搜索,我发现了许多结果。。。他们都没有解决我的问题。。但是从这些结果中,我知道了是什么导致了这个问题。。因此,对于每个有同样问题的人,我要这样做:

A/首先检查您的数据库(我的问题就在这里)。 在phpMyAdmin中创建数据库时,选择utf8\u general\u ci 请参见屏幕截图: 以下是我的表格结构:

B/转到java源代码并添加以下内容:(在我的例子中)

而不是:

 entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà"));
而不是发布“您的字符串”发布“urlcoder.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);
}
?>

没用..我想问题出在javacodeok,mybe。。。。在一个开发中,我遇到了同样的问题。。。“utf8_解码”帮我解决这个问题。如果您尝试“utf8\U解码”,但这无助于您解决问题。。所以很抱歉目前没有更多的解决方案。请参阅我的答案@ToukeaTatsi
  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  
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);
}
?>
<?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'");

?>