This happens because you are trying to do network activity in the main thread.
I had the same problem, it worked for a while, and then a few weeks after its development, it stopped working.
The solution I found is to add these lines to
onCreate()
Method:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
Hope this works for you too.
EDIT
Due to the increase in the number of upvotes, I want to add something. This will remove NetworkingOnMainThreadException , HOWEVER, this is far from the recommended method.
This exception exists for some reason. Network activity can take a lot of time, and network work on the main thread, which is the same thread that is responsible for updating the user interface, will freeze the thread until the network is completed (this is what happens in each thread, but when it runs on the selected thread, this is normal). On Android, if the UI thread is inactive for 5 seconds, it will display the Application is not responsive, Do you want to close it? dialog Application is not responsive, Do you want to close it? .
This is what the exception came about to prevent. Setting a policy, as I said, eliminates the exception, is the wrong way to perform actions. Any network actions must be performed in a separate thread, either using AsyncTask , or by manually creating a new Thread . AsyncTask is a very simple and easy way to implement this, and this is what I recommend.
Please consider this change when using my answer.
Greetings
La bla bla Jun 30 '12 at 22:14 2012-06-30 22:14
source share