2016-08-29 89 views
0

我有一個Android應用程序發送數據到數據庫,但在數據庫中某些字符存儲錯誤(例如: - > )。我已經得到了我的URLEncoders與UTF-8,數據庫也(如果我手動介紹「í」,「í」是corectctly存儲,所以我認爲這不是一個數據庫錯誤),我指出我的PHP腳本使用UTF-8,所以我不知道我可以嘗試更多。數據庫中沒有顯示的西班牙字符(「`」,「'」,「ñ」)

的Java

public class SendPostRequest extends AsyncTask<String, Void, String> { 

    protected void onPreExecute(){} 

    protected String doInBackground(String... arg0) { 

     try { 

      URL url = new URL("https://www.web.com/script.php"); // here is your URL path 

      JSONObject postDataParams = new JSONObject(); 
      postDataParams.put("user", user); 
      postDataParams.put("texto", edit_text_value); 
      postDataParams.put("l_origen", lengor_value); 
      postDataParams.put("l_destino", lengdest_value); 
      postDataParams.put("precio", precio); 
      postDataParams.put("num_pal", num_pal); 
      Log.e("params",postDataParams.toString()); 

      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(15000 /* milliseconds */); 
      conn.setConnectTimeout(15000 /* milliseconds */); 
      conn.setRequestMethod("POST"); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      OutputStream os = conn.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8")); 
      writer.write(getPostDataString(postDataParams)); 

      writer.flush(); 
      writer.close(); 
      os.close(); 

      int responseCode=conn.getResponseCode(); 

      if (responseCode == HttpsURLConnection.HTTP_OK) { 

       BufferedReader in=new BufferedReader(new 
         InputStreamReader(
         conn.getInputStream())); 

       StringBuffer sb = new StringBuffer("FUAAARK"); 
       String line=""; 

       while((line = in.readLine()) != null) { 

        sb.append(line); 
        break; 
       } 

       in.close(); 
       return sb.toString(); 


      } 
      else { 
       return new String("false : "+responseCode); 
      } 
     } 
     catch(Exception e){ 
      return new String("Exception: " + e.getMessage()); 
     } 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     Toast.makeText(getApplicationContext(), result, 
       Toast.LENGTH_LONG).show(); 
    } 
} 

public String getPostDataString(JSONObject params) throws Exception { 

    StringBuilder result = new StringBuilder(); 
    boolean first = true; 

    Iterator<String> itr = params.keys(); 

    while(itr.hasNext()){ 

     String key= itr.next(); 
     Object value = params.get(key); 

     if (first) 
      first = false; 
     else 
      result.append("&"); 

     result.append(URLEncoder.encode(key, "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(value.toString(), "UTF-8")); 

    } 
    return result.toString(); 
} 

PHP

$precio = $_POST['precio']; 
$texto = $_POST['texto']; 
$user = $_POST['user']; 
$l_origen = $_POST['l_origen']; 
$l_destino = $_POST['l_destino']; 
$num_pal = $_POST['num_pal']; 

define('HOST','***'); 
define('USER','***'); 
define('PASS','***'); 
define('DB','***'); 

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 

mysql_query("SET NAMES 'utf8'"); 

$sql = "INSERT INTO users (username, precio, text_cli, l_origen, l_destino, num_pal) VALUES('$user','$precio','$texto','$l_origen','$l_destino','$num_pal')"; 

謝謝你這麼多的寶貴時間

+0

嘗試使用相同的android請求與從終端捲曲,也許你可以找到如果問題是在android或在php方! –

回答

0

我已經做了一些調查,發現mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn);不存在,但mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn);已棄用。請糾正我,如果我錯了。

取而代之的是,我已經連接後,並查詢到DB之前以下放:

mysqli_set_charset($con, 'utf8'); 

這是工作的罰款。所以這裏是「完整的」代碼:

define('HOST','***'); 
define('USER','***'); 
define('PASS','***'); 
define('DB','***'); 

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 

mysqli_set_charset($con, 'utf8'); 

$sql = "INSERT INTO users (username, precio, text_cli, l_origen, l_destino, num_pal) VALUES('$user','$precio','$texto','$l_origen','$l_destino','$num_pal')"; 
1

觀光的PHP/MySQL/UTF-8

- The database tables and text columns should be set to UTF-8 

- HTML page Content-Type should be set to UTF-8 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

- PHP should send a header informing the browser to expect UTF-8 

header('Content-Type: text/html; charset=utf-8'); 

- The PHP-MySQL connection should be set to UTF-8 

mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn); 

mysqli_query("SET CHARACTER_SET_RESULTS='utf8'",$conn); 

mysqli_query("SET CHARACTER_SET_CONNECTION='utf8'",$conn); 

- PHP ini has default_charset setting it should be utf-8 if you do not have access to it use ini_set('default_charset', 'utf-8'); 
考慮
+0

嗨,謝謝你的回答。我嘗試了所有,但它不工作 – alberzyzz