Reputation: 711
I am currently developing an Android app that is using a remote MySQL database to write/read bookmarked by the user locations on the map. I acquire the address of a location via Reverse GeoLocation and after that pass it to a php file with the following code:
EditText addressEntry = (EditText) findViewById(R.id.addressentry);
String address = addressEntry.getText().toString().trim();
nameValuePairs.add(new BasicNameValuePair("address", address));
Here is my php file:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$mysqlid = mysql_connect("XXX","XXX","XXX");
mysql_select_db("XXX");
mysql_query("SET NAMES utf8", $mysqlid) ;
$insert_query = mysql_query("INSERT INTO table123 (userid, address) VALUES ('".$_REQUEST['userId']."', '".$_REQUEST['address']."') ");
mysql_close();
?>
</body>
</html>
If I submit an address that contains no-Cyrillic letters everything works fine - I get the record in the database. But if I submit a Cyrillic entry - I get the following record in the database :
. 13A
Sofia
Where every (it's a rectangle-symbol but not shown here - copy-pasted appears like this - ?) is a letter from the actual address. Before submitting the address to the database I have a toast showing it and it appears to be valid and readable.
Here is my code connecting the remote database:
public void onClick(View addBookmarkView) {
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userId", userIdPassed));
EditText addressEntry = (EditText) findViewById(R.id.addressentry);
String address = addressEntry.getText().toString().trim();
nameValuePairs.add(new BasicNameValuePair("address", address));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PHP_URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
I am sure the error is not in my database nor the php file - the sql database it has an Collation set to utf8_general_ci and if I try to put a static Cyrillic entry via another php file it works fine. Here the example:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$mysqlid = mysql_connect("XXX","XXX","XXX");
mysql_select_db("XXX");
mysql_query("SET NAMES utf8", $mysqlid) ;
$insert_query = mysql_query("INSERT INTO table123 (userid, address) VALUES ('123456789','асдсдасдас 123') ");
mysql_close();
?>
</body>
</html>
And this successfully adds a valid and readable in Cyrillic record in my database.
асдсдасдас 123
Do you have any idea if I miss to put something to specify encoding somewhere in the Android app code or maybe Manifest.xml or any other idea how to get this running? Cheers!
Upvotes: 1
Views: 1189
Reputation: 45942
Try to set the encoding for Cyrillic entries using this:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
Upvotes: 2