Android Java UTF-8 HttpClient Problem

后端 未结 5 2199
自闭症患者
自闭症患者 2020-12-01 04:35

I am having weird character encoding issues with a JSON array that is grabbed from a web page. The server is sending back this header:

Content-Type text/javascri

5条回答
  •  悲哀的现实
    2020-12-01 05:22

    @Arhimed's answer is the solution. But I cannot see anything obviously wrong with your convertStreamToString code.

    My guesses are:

    1. The server is putting a UTF Byte Order Mark (BOM) at the start of the stream. The standard Java UTF-8 character decoder does not remove the BOM, so the chances are that it would end up in the resulting String. (However, the code for EntityUtils doesn't seem to do anything with BOMs either.)
    2. Your convertStreamToString is reading the character stream a line at a time, and reassembling it using a hard-wired '\n' as the end-of-line marker. If you are going to write that to an external file or application, you should probably should be using a platform specific end-of-line marker.

提交回复
热议问题