Reading stream sometimes works but most of the time garbage is read...
843790Mar 6 2008 — edited Mar 6 2008I'm experiencing troubles with reading data from an (URL) stream. Most of the time the data that is read is garbage data, but SOMETIMES the data is read correctly. This only happens to a number of URL's (e.g. http://feeds.nos.nl/nosnieuws)
I have tried the following two pieces of code:
URL url = new URL("http://feeds.nos.nl/nosnieuws");
InputStream in = url.openStream();
////// piece one:
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1];
while (in.read(b) != -1) {
out.write(b);
System.out.println(Integer.toString(b[0], 16) + ":" + new String(b));
}
-----------
////// piece two:
Reader reader = new InputStreamReader(in);
char[] c = new char[1];
while (reader.read(c) != -1) {
System.out.println(c);
}
-----------
Both sometimes work but most of the time garbage is read... Are there people who experienced the same problems??? And how did you solve it???
Regards, :(