1

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?

Adil Soomro
  • 36,617
  • 9
  • 98
  • 146
BlueWanderer
  • 2,546
  • 2
  • 19
  • 35
  • Take a look at this: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – Paul Grime Feb 15 '12 at 10:58

1 Answers1

1

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 an InputStreamReader along with the InputStream you get by calling getInputStream().

If the response is not text-based, i.e. it's binary data, then the concept of charset is meaningless.

Graham Borland
  • 57,578
  • 20
  • 131
  • 176