Android : Caused by: android.os.NetworkOnMainThreadException
This question already has an answer here:
Add this in your onCreate():
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Use the above as a temporary solution. Otherwise use thread or asynctask.
You're attempting to make a network connection on the main (UI) thread. This is not allowed in Android since it will halt the entire UI until you get a response from the server.
Try using AsyncTask, or performing the connection on a separate thread.
Hope this helps.
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
链接地址: http://www.djcxy.com/p/29528.html