I am doing some experiments with android and PHP, the mobile device is a client posting some code via a HTTP POST
method to the PHP. I am using the following code:
URL url = new URL(host + webapp + syncURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setFixedLengthStreamingMode(postParams.getBytes().length);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
con.setDoOutput(true);
//send the POST out
PrintWriter out = new PrintWriter(con.getOutputStream());
out.print(postParams);
out.close();
The variable postParams
is in the following form:
Parámetros: hora=22%3A15%3A02&precision=25.1520004272461&fecha=2013-09-18&data=%5B%22S%C3%AD%22%5D
The original data is hora=22:15:02,precision=25.1520004272461,fecha=2013-09-18
and data=["Sí"]
stored as a key-value in a Map
object.
For transform the Map in postParams
I use the following code:
StringBuilder parametersAsQueryString = new StringBuilder();
try {
if (parameters != null) {
boolean firstParameter = true;
for (String parameterName : parameters.keySet()) {
if (!firstParameter) {
parametersAsQueryString.append(PARAMETER_DELIMITER);
}
Log.d(LOG_TAG, "Parámetro: " + parameterName + ", value: " + parameters.get(parameterName));
// Agregamos el nombre del parámetro
parametersAsQueryString.append(parameterName).append(PARAMETER_EQUALS_CHAR);
// Agregamos el valor del parámetro
try {
parametersAsQueryString
.append(URLEncoder.encode(parameters.get(parameterName), "UTF-8"));
} catch(NullPointerException npe) {
parametersAsQueryString
.append(URLEncoder.encode("", "UTF-8"));
}
firstParameter = false;
}
}
} catch (UnsupportedEncodingException uee) {
Log.d(LOG_TAG, "Error: " + uee);
But, in the data in the database is show as ["SÃ"]
. The database is a PostgreSQL
database with encoding UTF-8
, so I don't believe that the problem be there. Could someone help me? Thanks in advance.
Maybe Special characters are not allowed from android-to-webservice and viceversa.
If you have any special character/letter then you need to replace it with respective escape character sequence. Have a look here
I had the same problem with some french characters eg á
and i solved it by replacing á
with \á
eg if you want to print "Parámetros" then simply do.
String str="Parámetros";
str=str.replace("á","\u00e1");
Log.i("MyClass",str);
Now you can pass str
between two platforms!
Since String objects in Java don't handle utf-8, what you need to do is to send the data to PHP as a byteArray, you can use the ByteArrayWriter object instead of
PrintWriter out = new PrintWriter(con.getOutputStream());
You can use
ByteArrayWriter = new ByteArrayWriter(con.getOutputStream());
since in your header you already specify utf-8 encoding, PHP should understand it, just make sure to send the data as bytes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.