文档章节

NetworkOnMainThreadException异常处理

馨缘6009
 馨缘6009
发布于 2013/05/16 17:46
字数 414
阅读 21
收藏 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 is most commonly used to catch accidental disk or network access
on the application's main thread, where UI operations are received and
animations take place. Keeping disk and network operations off the main thread
makes for much smoother, more responsive applications. By keeping your
application's main thread responsive, you also prevent ANR dialogs from
being shown to users.

Note that even though an Android device's disk is often on flash
memory, many devices run a filesystem on top of that memory with very limited
concurrency. It's often the case that almost all disk accesses are fast, but may
in individual cases be dramatically slower when certain I/O is happening in the
background from other processes. If possible, it's best to assume that such
things are not fast.

Example code to enable from early in yourApplication,
Activity, or
other application component'sonCreate()
method:

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(newStrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()   // or .detectAll() for all detectable problems
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(newStrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());
     }
     super.onCreate();
 }


You can decide what should happen when a violation is detected. For example,
usingpenaltyLog()
you can watch the output ofadb logcatwhile you use your
application to see the violations as they happen.

If you find violations that you feel are problematic, there are a variety of
tools to help solve them: threads,Handler,AsyncTask,
IntentService,
etc. But don't feel compelled to fix everything that StrictMode finds. In
particular, many cases of disk access are often necessary during the normal
activity lifecycle. Use StrictMode to find things you did by accident. Network
requests on the UI thread are almost always a problem, though.

StrictMode is not a security mechanism and is not guaranteed to
find all disk or network accesses. While it does propagate its state across
process boundaries when doingBindercalls, it's
still ultimately a best effort mechanism. Notably, disk or network access from
JNI calls won't necessarily trigger it. Future versions of Android may catch
more (or fewer) operations, so you should never leave StrictMode enabled in
applications distributed on Google Play.

解决办法:直接在oncreate()方法中添加以下代码就可以了:
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());

© 著作权归作者所有

馨缘6009
粉丝 5
博文 75
码字总数 22716
作品 0
大同
程序员
私信 提问
okhttp3 怎么设置代理?

我的代码是这样的 报 NetworkOnMainThreadException 异常。

新宿豪雨
2016/08/31
2.2K
1
android点滴20: 4.0NetworkOnMainThreadException的解决方案

如果在Android4.0的主线程中进行比较耗时的操作,访问网络或下载就会报NetworkOnMainThreadException!要解决这个异常很简单,在主线程中新开一个线程来进行读写操作。但是还有一个更简单的解...

独鸣在彼岸
2013/06/07
253
0
浅入浅出Android(014):HTTP GET获取文本内容

Android studio版本:0.8.6;虚拟机版本:4.*。 1、建立android项目MyApplication 2、修改布局文件activity_my.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and......

樂天
2014/09/29
1K
4
android之android.os.NetworkOnMainThreadException异常

在MainActivity中加载网络图片,代码如下: public class NetImageActivity extends Activity { String imageUrl = "http://content.52pk.com/files/100623/22301024371_lit.jpg"; Bitmap bm......

有资本再款
2015/10/19
2.4K
0
异常:android.os.NetworkOnMainThreadException

Android 4.1项目:使用新浪微博分享时报:   android.os.NetworkOnMainThreadException 网上搜索后知道是因为版本问题,在4.0之后在主线程里面执行Http请求都会报这个错,也许是怕Http请求...

IT妖姬
2014/04/18
0
0

没有更多内容

加载失败,请刷新页面

加载更多

聊聊nacos的LocalConfigInfoProcessor

序 本文主要研究一下nacos的LocalConfigInfoProcessor LocalConfigInfoProcessor nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java p......

go4it
昨天
1
0
前端技术之:webpack热模块替换(HMR)

第一步:安装HMR中间件: npm install --save-dev webpack-hot-middleware 第二步:webpack配置中引入webpack对象 const webpack = require('webpack’); 第三步:增加devServer配置项: ho......

popgis
昨天
2
0
死磕 java线程系列之线程池深入解析——体系结构

(手机横屏看源码更方便) 注:java源码分析部分如无特殊说明均基于 java8 版本。 简介 Java的线程池是块硬骨头,对线程池的源码做深入研究不仅能提高对Java整个并发编程的理解,也能提高自己...

彤哥读源码
昨天
3
0
虚函数表 图解

虚函数表 图解 p504

天王盖地虎626
昨天
2
0
java反射

学习目标  什么是反射  反射运行原理  了解反射机制的相关类  获取 class 对象的 3 种方式  通过反射获取构造方法并使用  通过反射获取成员变量并调用  通过反射获取成员方法并...

流川偑
昨天
3
2

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部