Android BufferedReader does not read the whole response

650 views Asked by At

I have a problem with reading a ULR response. On Android it only reads around the half of the response.

If I use the same code in a normal Java project everything works fine.

    try {
        String _output = null;
        URL url = new URL("http://example.com");
        BufferedReader buffer = new BufferedReader(new InputStreamReader(url.openStream()));

        StringBuilder everything = new StringBuilder();
        String line;
        while ((line = buffer.readLine()) != null) {
            everything.append(line);
        }
        _output = everything.toString();

        buffer.close();

        System.out.print(_output);
    } catch (IOException e) {
        e.printStackTrace();
    }
1

There are 1 answers

1
Lolo On BEST ANSWER

How do you know that it's only half of the response? If you rely on what is printed with System.out.println() then you should be aware that Logcat has a limitation that prevents it from printing more than 4,000 characters. Anything after that is truncated. To check how much of the response you have, you could print everything.length()first, ot see if you are in that situation.

You can look at this existing question on SO for reference, but there are many others.