Quick fix for NetworkOnMainThreadException

前端 未结 3 1544
轻奢々
轻奢々 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: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);
    

提交回复
热议问题