Quick fix for NetworkOnMainThreadException

前端 未结 3 1528
轻奢々
轻奢々 2020-12-03 22:01

I need to execute third-party open source program, which throws NetworkOnMainThreadException. According to SDK reference, this is only thrown for applications targeting the

相关标签:
3条回答
  • 2020-12-03 22:27

    These are not solutions, you Just tell the tutelar to sleep and sill!!! the android decide not to let you make network operations in the main thread (UI thread), because of the "halts" that these operation can make (the ui stop respond). So, as I read, they tell you to not shoot your leg, and you find the way to do!!!

    the solution : separated thread, Or Async ...

    0 讨论(0)
  • 2020-12-03 22:42

    You could change it to:

    android:targetSdkVersion="9"
    

    API 10 corresponds to honeycomb, while 9 is gingerbread. This behavior is only seen in APIs 10 and above.

    However, I would advise against this. Instead, you should move any long running operations, or operations with the possibility of running for long into a background thread, like an AsyncTask.

    You could also try to set Strict Mode off using:

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    
    0 讨论(0)
  • 2020-12-03 22:50

    You are performing network task on your UI Thread. The best way to perform network operation is to use AsyncTask or simply create another thread. However, if you want a quick and dirty way to get rid of the error; you can also use this

    ThreadPolicy tp = ThreadPolicy.LAX;
    StrictMode.setThreadPolicy(tp);
    
    0 讨论(0)
提交回复
热议问题