简体   繁体   English

Android无法插入带有重音和空格的数据库字符串

[英]Android cannot insert into database string with accents and blank space

I´m having a problem with this string: "Não Vou". 我对此字符串有疑问:“NãoVou”。 It means "I don´t go". 意思是“我不去”。 I´m trying to insert it into database with php/json file. 我正在尝试使用php / json文件将其插入数据库。

When I remove blank space and ~ accent it works fine. 当我删除空格并加重音时,它可以正常工作。

What I have tried: 我试过的

  • changed iso-8859-1 to utf-8; 将iso-8859-1更改为utf-8;
  • changed eclipse encoding to utf-8; 将eclipse编码更改为utf-8;
  • changed print(json_encode($flag)) to print(json_encode('utf8_encode', $flag)); 将print(json_encode($ flag))更改为print(json_encode('utf8_encode',$ flag));
  • Have read similar posts but any of them helped me. 阅读过类似的帖子,但其中任何一个都对我有所帮助。

     public class insertNo extends AsyncTask<String, Boolean, Boolean>{ @Override protected Boolean doInBackground(String... params) { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("e_id", subString)); nameValuePairs.add(new BasicNameValuePair("Não Vou", no)); try{ HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://mywebsite.com/includes/insert_counter.php"); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); inputStream = entity.getContent(); Log.e("pass 1", "connection success"); }catch(Exception e){ Log.e("Fail 1", e.toString()); Log.e("Invalid IP", e.toString()); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { stringBuilder.append(line + "\\n"); } inputStream.close(); result = stringBuilder.toString(); Log.e("pass 2", "connection success"); } catch (Exception e) { Log.e("Fail 2", e.toString()); } try { JSONObject json_data = new JSONObject(result); code = (json_data.getInt("code")); if (code==1) { Log.d("Insert success", result); } else { Log.d("Sorry son", result); } } catch (Exception e) { Log.e("Fail 3", e.toString()); } return null; } 

    } }

php file php文件

include_once("db_connect.php");

$e_id = $_REQUEST["e_id"];
$yes = $_REQUEST["Vou"];
$no = $_REQUEST["Não Vou"];
$maybe = $_REQUEST["Talvez"];

$flag['code']=0;


if(isset($yes)){
    $r = mysqli_query($mysqli, "UPDATE table1 SET yes = yes + 1 WHERE id='$e_id'");
    $flag['code']=1;
}else if(isset($no)){
    $r = mysqli_query($mysqli, "UPDATE table1 SET no = no + 1 WHERE id='$e_id'");
    $flag['code']=1;
}else if(isset($maybe)){
    $r = mysqli_query($mysqli, "UPDATE table1 SET maybe = maybe + 1 WHERE id='$e_id'");
    $flag['code']=1;
}

print(json_encode($flag));
mysqli_close($mysqli);

根据文档 ,PHP变量不应包含特殊字符,空格或其他任何字符

a letter is a-z, A-Z, and the bytes from 127 through 255 (0x7f-0xff). 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM