I recently asked this question on SO: Grabbing JSON works from one link, not from another
I got an answer basically saying that getContentLength
is optional by the server, so I would have to manually count the length myself. Well, it took me some time but I came up with some code that seems to get the right count, but I'm not sure if it's the "right" way to do it. Mostly because of my while(0==0)
statement.
The exact answer I got was:
content-length is a header set by the responding server. It is optional, and google chooses not to send it for dynamically generated content (most servers don't). So you'll have to read the stream until you're out of bytes, rather than doing it in one go.
So this is my code that I came up with:
Reader reader = new InputStreamReader(inputStream);
int contentLength=0;
int cur;
while (0==0){
cur = reader.read();
if (cur == -1) {
break;
} else {
contentLength++;
}
}
Does that seem like a viable solution for counting the content length when the server doesn't provide you with one?