NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.
You should call method on asynctask then only code will work. To avoid it you should call it on another thread. Hence asynctask is better.
http://android-developers.blogspot.in/2009/05/painless-threading.html
http://android-er.blogspot.in/2012/04/androidosnetworkonmainthreadexception.html
http://www.lucazanini.eu/2012/android/the-android-os-networkonmainthreadexception-exception/?lang=en
here is link that illustrates how to use asynctask