Android 3.0 访问WebService 出现 android.os.NetworkOnMainThreadException异常

今天在用android访问.NET 服务器的时候,出现了这个错误:android.os.NetworkOnMainThreadException,在网上找了下发现是android版本问题,然后找到了个解决方法,感觉还行。如果你是在真机中调试,只要前面7行就可以了。

 1 public void onCreate() {  
 2      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()     
 3             .detectDiskReads()     
 4             .detectDiskWrites()     
 5             .detectNetwork()   // or .detectAll() for all detectable problems     
 6             .penaltyLog()     
 7             .build());     
 8      StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()     
 9             .detectLeakedSqlLiteObjects()     
10             .detectLeakedClosableObjects()     
11             .penaltyLog()     
12             .penaltyDeath()     
13             .build());  
14 }  
原文地址:https://www.cnblogs.com/wuyihong/p/2466191.html