I am getting the error “android.os.networkonmainthreadexception” while trying to pull a json data from an url.
Would some tell me why?
I am getting the error android.os.networkonmainthreadexception while trying to pull a json data from
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Its due to the StrictMode.ThreadPolicy. It was introduced since API Level 9 and the default thread policy had been changed since API Level 11, which in short, does not allow network operation (eg: HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException.
The recommended way of solving this is by Using an AsyncTask so that the network request does not block the UI thread.
You can override this thread policy by adding the below code into your main activity’s onCreate() method.