Different behavior of XmlPullParser.getInputEncoding() on API11+ and pre-API11 versions of Android

前端 未结 2 802
无人及你
无人及你 2021-01-16 08:59

I am developing a new feature for my android app to enable data backup and restore. I am using XML files to backup data. This is a piece of code that sets encoding for an ou

2条回答
  •  一个人的身影
    2021-01-16 09:57

    FileReader and other readers don't detect encoding. They just use the platform default encoding which can be UTF-8 by coincidence. It has no relation to the actual encoding of the file.

    You cannot detect XML file encoding until you read it enough to see the encoding attribute.

    From getInputEncoding() documentation

    if inputEncoding is null and the parser supports the encoding detection feature, it must return the detected encoding

    And:

    If setInput(Reader) was called, null is returned.

    So it appears that pre 11 doesn't support detection which is enabled by using setInput(is, null). I don't know how you are getting "UTF-8" when using setInput(reader) as the documentation says it should return null.

    Then:

    After first call to next if XML declaration was present this method will return encoding declared.

    So in pre 11, you could try calling .next() intially before calling .getInputEncoding

提交回复
热议问题