开发者

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, list开发者_如何学Pythoning 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?


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.


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.


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜