alberzyzz alberzyzz - 3 months ago 19
Java Question

Spanish characters ("`", "´", "ñ") not showing in database

I have got an Android app that send data to a database but in the database some characters are stored wrongly (example: í -> í).

I have got my URLEncoders with UTF-8, the database too (if I manually introduce "í", "í" is corerctly stored so I think it is not a database fault) and I indicated in my php script to use UTF-8 so I do not know what more could I try.

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')";


Thank you so much for your time

Answer

I have done some research and found that mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn); does not exist, but mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn); which is deprecated. Please, correct me if I am wrong.

Instead, I have put after connection and before the query to the DB the following:

mysqli_set_charset( $con, 'utf8');

Which is working fine. So here is the "complete" code:

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')";