血的教训,一定不要再4.0以后在主线程里面访问网络NetworkOnMainThreadException

来源:互联网 发布:魔兽争霸3mac原生版 编辑:程序博客网 时间:2024/06/05 08:00

尼玛,搞了好久好久,在主线程里面解析xml,在2.3中完全没有问题,在4.0中就会出现错误 NetworkOnMainThreadException  ,气死了  我还以为是xml解析方法不对,sax,dom,pull都试过了还是不行  最后在网上找到了答案  郁闷啊

1.异步访问网络

2.

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() // or// .detectAll()// for// all// detectable// problems.penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());

写在主线程里面

血的教训啊!!!

原创粉丝点击