I’m developing applications for android devices and had a problem while developing lately.
I needed to get information out of an html-file online, so I made a construct of InputStream and BufferedReader to actually scan the file for information. I splitted my string to actually get my information and tried displaying it with a toast.
Everything works fine and the way I want it to, but everytime a special-characters should be displayed, a questionmark-hash is.
I think it might be a problem of the charset, because the website say in the :
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
How to I get this right?
EDIT :
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials("user","password"));
HttpResponse response;
response = httpClient.execute(post);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null) {
Toast.makeText(this, line, Toast.LENGTH_LONG).show();
}
InputStreamReadermay actually take aCharsetas a second parameter, to indicate, I presume, the character encoding of the stream it’s going to read. Standard-compliant Java implementations are not required to feature thewindows-1252encoding, but I believe it’s quite similar toISO-8859-1, which you can try as a first workaround to see if it works. There’s also another possibly interesting constructor in theInputStreamReaderclass, taking aCharsetDecoderas a second parameter (you can create one by invokingCharset.newDecoder), which you may try to use to decode the stream in the encoding you prefer, or perhaps in the system’s default encoding, that you can obtain by invokingCharset.defaultCharset.See the JavaDoc API documentation for InputStreamReader, Charset and CharsetDecoder for details. Indeed I’m not an expert and I know just a little about encoding and its issues, but I thought it worth to point out the availability of these classes.
You may also check the encoding used for the
InputStreamReaderby invoking itsgetEncodingmethod.