0

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.

2
  • What do you see when you print (value of data) this character on console/log on the server side? Commented Sep 19, 2013 at 7:01
  • @Santosh what I see is as showed in the question, the line that begins with Parámetros: is from the console Commented Sep 19, 2013 at 13:56

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 e.g.á and i solved it by replacing á with \u00e1

e.g. 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!

1 Comment

I will try your solution tonigh :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.