44

I am working on project which includes communication of the server (JavaEE app) and client (Android app). XML is sent as one of POST parameters of the HTTP request (named "xml"). There are also few other POST parameters which I pass to server, but in function below I removed them for simplicity. Problem that occurs is that certain letters are not properly delivered to the server - for example character Ű (Note that this is not German Ü, which is properly delivered, by the way). Code for sending is the following:

private String postSyncXML(String XML) {
    String url = "http://10.0.2.2:8080/DebugServlet/DebugServlet";
    HttpClient httpclient = new DefaultHttpClient();  

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("xml",XML));

    UrlEncodedFormEntity form;
    try {
        form = new UrlEncodedFormEntity(nameValuePairs);
                form.setContentEncoding(HTTP.UTF_8);
        HttpPost httppost = new HttpPost(url);

        httppost.setEntity(form);

        HttpResponse response = (HttpResponse) httpclient .execute(httppost);
        HttpEntity resEntity = response.getEntity();  
        String resp = EntityUtils.toString(resEntity);
        Log.i(TAG,"postSyncXML srv response:"+resp);
        return resp;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

My guess is that problem is in the BasicNameValuePair I use to set XML as one of POST parameters, and it's documentation says it uses US-ASCII character set. What is the proper way to send UTF-8 encoded POST fields?

Tuna
  • 2,937
  • 4
  • 37
  • 61
dstefanox
  • 2,222
  • 3
  • 19
  • 21
  • could you please post the form.toString(); after setting the contentEncoding? And it would also be usefull to get a complete print output of the nameValuePairs list. I think if we have a look at these two we can see the problem. – Chris Mar 11 '11 at 08:51
  • It was not as trivial as using form.toString() to get how content is encoded, but result is the following: instead of encoding mentioned letter as sequence of bytes "C5 B0", it is encoded as "1A". So, it seems that form is not properly encoding this character... – dstefanox Mar 11 '11 at 10:51
  • This solution can solved my problem, Try this one : http://stackoverflow.com/a/6228377/1929078 – Amintabar Sep 06 '14 at 13:56

6 Answers6

102

After much research and attempts to make things working, I finally found a solution for the problem, that is a simple addition to existing code. Solution was to use parameter "UTF-8" in the UrlEncodedFormEntity class constructor:

form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");

After this change, characters were encoded and delivered properly to the server side.

dstefanox
  • 2,222
  • 3
  • 19
  • 21
  • 1
    +1 This is what helped me when sending POST requests from Android to PHP scripts on Heroku that work with PostgreSQL. You have to ensure the data is encoded in UTF-8, which this solution does. Thanks a lot! – caw Aug 14 '13 at 03:05
  • This solution is working fine if we are sending just text to the server, but if we want to send images then we need to use MultipartEntity for that. I am using the below code to set Encoding in MultipartEntity but it is not working. new MultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"), new MultiPartEntity.ProgressListener() { @Override public void transferred(long num) { } }); – Prithniraj Nicyone Jan 04 '16 at 08:06
22

When you do this line

form = new UrlEncodedFormEntity(nameValuePairs);

you need to specify the charset like this

form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");

You can go to Android Developer find out.

Constructs a new UrlEncodedFormEntity with the list of parameters with the default encoding of DEFAULT_CONTENT_CHARSET

BobGao
  • 770
  • 8
  • 18
3
String finalString = URLEncoder.encode(request, "UTF-8");

return finalString;

user finalString in your post method.

AZ_
  • 21,688
  • 25
  • 143
  • 191
1

Or I can add below code in scriptlet at the top of my test2.jspx which will solve the issue


   
     String en = request.getCharacterEncoding();
     if(en == null) {
      request.setCharacterEncoding("UTF-8");
     }
   
Aditya Sanghi
  • 13,370
  • 2
  • 44
  • 50
0

This has been the problem Sending UTF-8 data from Android. Your code would work fine except that you will have to encode your String to Base64 . At Server PHP you just decode Base64 String back. It worked for me. I can share if you need the code.

Mukund K Roy
  • 175
  • 1
  • 8
0

I also faced similar issue. But to verify it, I wrote below two JSPs

-------------test1.jspx-----------------

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
  <jsp:directive.page contentType="text/html; charset=utf-8"/>
<body>
      <form action="/test2.jspx" method="POST" accept-charset="UTF-8">
                            <input type="text" name="u" id="u" />
                            <input type="submit" value="Login3" />
    </form>
</body>
</html>

-------------test2.jspx-----------------
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">

<jsp:directive.page contentType="text/html; charset=utf-8"/>
<body>
    The test entered is <jsp:expression>request.getParameter("u")</jsp:expression>
</body>
</html>
----------------------------------

And then entered below accented characters in first input box ÂÃÄÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ

And the issue is clearly visible that Android browser can not handle the UTF-8 encoding in POST parameters.

I think, I will need to use GET method and will need to add "URIEncoding=UTF-8" for connector in tomcat server.xml.