On an Android device, could an HttpURLConnection recognize the charset of its response automatically?
That is, if I have received some plain text response through an HttpURLConnection, can I get a right String (or maybe a right Reader) without knowing the charset used to encode the response?
You can call
getContentType()which returns the responses MIME type. If it’s a text-based response then this may include the character set, which you can then extract and pass to anInputStreamReaderalong with theInputStreamyou get by callinggetInputStream().If the response is not text-based, i.e. it’s binary data, then the concept of charset is meaningless.