Sunday 20 January 2013

NetworkOnMainThreadException

One of the recent issues I found while working with Android 4.0 is NetworkOnMainThreadException.
This is only thrown for applications targeting the Honeycomb SDK (3.0) or higher. Applications targeting earlier SDK versions are allowed to do networking on their main but it's heavily discouraged.

There are some other operations that jelly bean, ICS, HoneyComb won't allow to perform on the UI thread like:
  • HTTP requests (i.e. HTTPClient and HTTPUrlConnection).
  • Opening a Socket connection (i.e. new Socket()).
  • Attempting to connect to a remote MySQL database.
  • Downloading a file (i.e. Downloader.downloadFile()).
 It is recommended not to performing long operations such as network access, database queries or accessing disk on the UI thread.

Solution:
There are three ways to solve the problem of NetworkOnMainThreadException.
  • If you look at android document you will find that from Honeycomb (3.0) they have introduced StrictMode which will not allow network operations on UI thread. So one simple solution is disabling the StrictMode. 
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

But I will not recommend this solution as it will increase load on UI thread with expensive operations like network operation which may freeze your app's UI and freezing UI or lagging in UI will give bad user experience.

  • Second solution is to use AsyncTask<Params, Progress, Result> , using async task you can do all the long running operations which may result in UI lagging if you do those operations on UI thread.
  • Third solution is to use Service. for network operations. Where you required to download data once only when user starts the app in that case I recommend IntentService.
 I hope this will help you, if any body wants to know more about how to use AsyncTask, IntentService or bind Service then please let me know, I will try to help on these.

15 comments:

  1. Hi Suru,

    Thanks a lot, this is really useful information.

    ReplyDelete
  2. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    iot-training-in-chennai

    ReplyDelete
  3. A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.


    Data Science training in marathahalli
    Data Science training in btm
    Data Science training in rajaji nagar
    Data Science training in chennai
    Data Science training in electronic city
    Data Science training in USA
    Data science training in pune
    Data science training in kalyan nagar

    ReplyDelete
  4. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
    python online training
    python training in OMR
    python training in tambaram

    ReplyDelete
  5. I recently came across your blog and have been reading along. I thought I would leave my first comment.
    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  6. I would assume that we use more than the eyes to gauge a person's feelings. Mouth. Body language. Even voice. You could at least have given us a face in this test.


    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  7. I have picked cheery a lot of useful clothes outdated of this amazing blog. I’d love to return greater than and over again. Thanks! 


    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  8. Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.


    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  9. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers


    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    ReplyDelete