Android StrictMode – NetworkOnMainThreadException

I have writen about StrictMode before.

In case you have not noticed; StrictMode for network access results in a fatal error as for Android 3.0 (Honeycomb) or later, unless your app is targeting an API version before Honeycomb.

The right way of solving this is to use Android AsyncTask for network access.

The lazy way of handling this is to turn the check of:

[source code=”java”]
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
[/source]

For my Android Network tutorial I use the lazy way because I don’t want to force the reader to learn both things at the same time.

But I recommend you learn Android Background Processing.

I hope this helps.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences,

He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.

This entry was posted in Android and tagged , , , . Bookmark the permalink.