The NetworkOnMainThread exception occurs because you have called some network operation on the default thread, that is, the UI thread. As per Android version Android 3 (Honeycomb) which is not allowed, you should call network operation outside the main thread.
You can use AsyncTask, IntentService, or creating your own thread and calling inside the run method. For more information, visit Connecting to the Network.