Yesterday, a project ran on android 2.3, but not on android 4.0. Run on android 4.0 to report to android. OS. networkOnMainThreadException exception. The cause is queried today. Originally in 4.0, the access network cannot be performed in the main program. There are two solutions, one being added to the main program:
I:
// For details, see StrictMode. setThreadPolicy (new StrictMode. threadPolicy. builder (). detectDiskReads (). detectDiskWrites (). detectNetwork () // or. detectAll () for all detectable problems. penaltyLog (). build (); StrictMode. setVmPolicy (new StrictMode. vmPolicy. builder (). detectLeakedSqlLiteObjects (). detectLeakedClosableObjects (). penaltyLog (). penaltyDeath (). build ());
II:
The other is to start the thread to execute the download task:
Public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // start the Thread to execute the download task new Thread (downloadRun ). start ();}/*** download thread */Runnable downloadRun = new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubupdateListView ();}};