android.os.NetworkOnMainThreadException问题
2013-03-27 16:17
561 查看
最近测试程序在手机端测试正常,在联网的时候总会抛出android.os.NetworkOnMainThreadException这个异常
也就是说不能在主线程中执行联网操作
在4.0中,访问网络不能在主程序中进行,有两个方法可以解决,一个是在主程序中增加:
StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.
// 详见StrictMode文档
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());
另一种是启动线程执行下载任务:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**
* 下载线程
*/
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateListView();
}
};
也就是说不能在主线程中执行联网操作
在4.0中,访问网络不能在主程序中进行,有两个方法可以解决,一个是在主程序中增加:
StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.
// 详见StrictMode文档
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());
另一种是启动线程执行下载任务:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 启动线程执行下载任务
new Thread(downloadRun).start();}
/**
* 下载线程
*/
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateListView();
}
};
相关文章推荐
- 问题描述: android.os.NetworkOnMainThreadException异常(不要在主线程访问网络资源)
- 关于连接网络时抛出“android.os.NetworkOnMainThreadException”异常问题
- 【Internet】android.os.NetworkOnMainThreadException问题
- 【Android 错误记录】android.os.NetworkOnMainThreadException 异常问题
- android网络请求问题:android.os.NetworkOnMainThreadException
- 老出现这样问题现在做个记录:android.os.NetworkOnMainThreadException
- android4.0Caused by: android.os.NetworkOnMainThreadException问题
- 开发中碰到的问题:android.os.NetworkOnMainThreadException
- 安卓问题总结( android.os.NetworkOnMainThreadException异常)
- 手把手教你解决android网络请求问题:android.os.NetworkOnMainThreadException
- 如何解决安卓网络主线程异常的问题:Caused by: android.os.NetworkOnMainThreadException [duplicate]
- android.os.NetworkOnMainThreadException问题
- Android Eclipse工程开发中的常见调试问题(二)android.os.NetworkOnMainThreadException 异常的解决办法
- android.os.NetworkOnMainThreadException问题
- 【边做项目边学Android】异常处理:android.os.NetworkOnMainThreadException--多线程问题
- Android多线程问题。android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException 异常处理
- Android3.0版本以上出现android.os.NetworkOnMainThreadException异常与处理方式
- [Android开发那点破事]解决android.os.NetworkOnMainThreadException
- 异常:android.os.NetworkOnMainThreadException