Android Java UTF-8 HttpClient Problem

后端 未结 5 2195
自闭症患者
自闭症患者 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:18

    Extract the charset from the response content type field. You can use the following method to do this:

    private static String extractCharsetFromContentType(String contentType) {
        if (TextUtils.isEmpty(contentType)) return null;
    
        Pattern p = Pattern.compile(".*charset=([^\\s^;^,]+)");
        Matcher m = p.matcher(contentType);
    
        if (m.find()) {
            try {
                return m.group(1);
            } catch (Exception e) {
                return null;
            }
        }
    
        return null;
    }
    

    Then use the extracted charset to create the InputStreamReader:

    String charsetName = extractCharsetFromContentType(connection.getContentType());
    
    InputStreamReader inReader = (TextUtils.isEmpty(charsetName) ? new InputStreamReader(inputStream) :
                        new InputStreamReader(inputStream, charsetName));
                BufferedReader reader = new BufferedReader(inReader);
    

提交回复
热议问题