Java UTF-8 encoding not working HttpURLConnection

后端 未结 3 757
无人及你
无人及你 2021-01-03 23:30

I tried to do post call and to pass input with this value - \"ä€愛لآहที่\" I got error message

{\"error\":{\"code\":\"\",\"message\":{\"lang\":\"en-US\",\"va         


        
相关标签:
3条回答
  • 2021-01-04 00:07

    You have to send content via byte array

     DataOutputStream outputStream= new DataOutputStream(conn.getOutputStream());
     outputStream.write(content.toString().getBytes());
    

    This is completely solution for your file name character problems. The imported point is string sending via byte array. Every character changing via byte character. This is prevent your character encoding problems.

    0 讨论(0)
  • 2021-01-04 00:09

    It seems that your variable content does already have the wrong data because you may have converted a String without any attention to the required encoding.

    Setting the correct enconding on the writer and use write() instead of writeBytes() should be worth a try.

    0 讨论(0)
  • 2021-01-04 00:10

    Please try this code below:

    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
    writer.write(content);
    writer.close();
    wr.close();
    

    You should use JSONObject to pass params

    The input, please try

    BufferedReader reader = new BufferedReader(new InputStreamReader(resultContentIS, "UTF-8"));
    

    If the out put is: ???????, so do not worry because your output console do not support UTF-8

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