[error] android.os.networkonmainthreadexception

You cannot perform network IO on the UI thread on Honeycombe. Technically it is possible on earlier versions of Android, but is a really bad idea as it will cause your app to stop responding, and can result in the OS killing your app for being badly behaved. You’ll need to run a background process or use AsyncTask to perform your network transaction on a background thread.

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:

在android 2.3上设计的下载程序,在android 4.0上运行时报android.os.NetworkOnMainThreadException异常,原来在4.0中,访问网络不能在主程序中进行。从 Android 2.3 开始提供了一个新的类 StrictMode,该类可以用于捕捉发生在应用程序主线程中耗时的磁盘、网络访问或函数调用,可以帮助开发者改进程序,使主线程处理 UI 和动画在磁盘读写和网络操作时变得更平滑,避免主线程被阻塞。

有两个方法可以解决,一个是在主程序中增加判断:

protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub,It means that your IDE (or some other tool) has automatically generated a method for you, but has left the body blank to be filled in by you (this is known as a “stub“).
String strVer=GetVersion.GetSystemVersion();
strVer=strVer.substring(0,3).trim();
float fv=Float.valueOf(strVer);
if(fv>2.3)
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // 这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
.penaltyLog() //打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() //探测SQLite数据库操作
.penaltyLog() //打印logcat
.penaltyDeath()
.build());
}
super.onCreate(savedInstanceState);

}

 

另一种是启动线程执行下载任务

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();
}
};

via

android.os.NetworkOnMainThreadException 异常处理

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: