Post UTF-8 encoded data to server loses certain characters

后端 未结 6 1853
旧巷少年郎
旧巷少年郎 2020-12-02 12:31

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 \"

相关标签:
6条回答
  • 2020-12-02 12:43

    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.

    0 讨论(0)
  • 2020-12-02 12:44

    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.

    0 讨论(0)
  • 2020-12-02 12:54

    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");
         }
       
    
    0 讨论(0)
  • 2020-12-02 12:57

    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

    0 讨论(0)
  • 2020-12-02 13:03

    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.

    0 讨论(0)
  • 2020-12-02 13:08
    String finalString = URLEncoder.encode(request, "UTF-8");
    
    return finalString;
    

    user finalString in your post method.

    0 讨论(0)
提交回复
热议问题