Android Eve
Android Eve

Reputation: 14974

Pro Android 2: What does system.out.println in Android?

To the best of my (current) understanding, Android has no console to send the messages to so the System.out.println messages get lost. Instead, Log.x (outputing to LogCat) should be used.

Yet, in the acclaimed Pro Android 2 book, listing 8-1 does just that:

    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer sb = new StringBuffer("");
    String line = "";
    String NL = System.getProperty("line.separator");
    while ((line = in.readLine()) != null) {
        sb.append(line + NL);
    }
    in.close();

    String page = sb.toString();
    System.out.println(page);

Does System.out.println really work in Android or is it only a typo?

If the former (i.e. not a typo), what does it really do and where should I expect to find the output?

Upvotes: 3

Views: 3536

Answers (4)

Marcio Mangar
Marcio Mangar

Reputation: 156

If you're using Eclipse, System.out will be displayed on the LogCat tab not in the Console. You can also try the Log.i("MyLog", "The message here...."); also displayed on the LogCat.

Enjoy!

Upvotes: 0

Stephan Branczyk
Stephan Branczyk

Reputation: 9375

That System.out.println code does work (despite what the android documentation says). I've tried it on my on my Android development set up (without changing the settings as they describe in the documentation). I've had tried on the set ups of my Android programming students, both on their Windows and Macs. And it always comes out the same way:

The following line does output to logcat by default.

System.out.println("blah blah");

Now of course, it's probably best to stick to Log.x() anyway. It's probably never a good idea to rely on undocumented features, especially in a book. This feature could be here today, and be just as well gone the next time the tools get updated.

Upvotes: 2

Cheryl Simon
Cheryl Simon

Reputation: 46844

System.out.println prints to whatever is currently defined as the "standard" output PrintStream. It is possible to hook this up to point to the log stream, but really there is no good reason to use it. Use Log.x.

This is probably just a typo in the book. Nothing in that code snippet is Android specific.

Upvotes: 3

Robert Massaioli
Robert Massaioli

Reputation: 13487

From the android docs:

By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null.

It also explains how to use System.out and System.err properly.

Upvotes: 1

Related Questions