在开发使用网络连接的android应用时,遇到这么一个问题,只要一连接网络就报下面这个异常
android.os.NetworkOnMainThreadException
查了下API和源码,发现原来在4.0之后,使用网络时都会用到
StrictMode
中 AndroidBlockGuardPolicy的检查策略,如果在UI线程连接网络就会报这个异常,尝试了一下把AndroidMainfest中android:targetSdkVersion="15"去掉或者更换成更低的版本,这个异常就不会报了。最根本的解决办法是把这些网络操作放到非UI线程去执行。
在API文档中这一点也有了说明,这样也好,避免我们把耗时的网络操作放到UI线程中去执行,提高应用的用户体验。
在此记一下,免得以后忘记,也分享出来,希望朋友们不会再范同样的错误了。
API说明贴出来:
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.
- 大小: 89.9 KB
分享到:
相关推荐
主要介绍了Caused by: android.os.NetworkOnMainThreadException错误解决办法,本文提供了2种解决方法,需要的朋友可以参考下
android.os.NetworkOnMainThreadException 解决事情应该优先解决一件事情,总是三心二意,最后塞翁失马 文章目录android.os.NetworkOnMainThreadException问题分析查阅资料解决方案知识要点: RunOnUiThread 和 new...
在Android中,如果想退出Android程序,一般都是调用finish()、System.exit(0)android.os.Process.killProcess(android.os.Process.myPid())等方法来实现退出程序功能,可是在实际开发中,并不能达到完全退出应用程序...
最近在Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appName-2.3.0.apk exposed beyond app through Intent.getData(),通过查找相关的资料终于找到了...
cannot find symbol import android.os.SystemProperties 前言 最近在搞Android高版本的9和10开发,正在敲着代码唱着歌,发现在Android源码目录的app下面导入import android.os.SystemProperties这个包老是不行...
This is a development tool to show values of android.os.Build.* Try it Installing android SDK See the official android doc Create emulator The command line for creating an AVD has the following syntax...
Android webview 遇到android.os.FileUriExposedException错误解决办法 1. 在 Manifest 文件中添加: <provider android:name=android.support.v4.content.FileProvider android:authorities=${...
Apress.Pro.Android.Web.Apps.Developing.HTML5.JavaScript.CSS.and.Chrome.OS.Web.Apps
android.util.Base64类
使用android.hardware.camera2写的新的自定义相机
当出现提示说: Call requires API level 9 (current min is 8): android.os.StrictMode#setThrea 也就是说版本低
完整的android.support.annotation.jar包含Nullable、Keep、XmlRes、TransitionRes、WorkerThread等共49个类
为apk签android.uid.system名工具包signapk.jar,platform.x509.pem,platform.pk8
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android....
问题: 在百度地图页面(Activity为singleTask)跳转到别的页面 ,离开地图页面。偶发性空指针,具体日志信息如下: 解决: 1、原本我在生命周期onStop里调用的mlocationClient.stop();...作者:Android_Mr_Zhao
或许困扰很多Android开发者最大的问题莫过于Bug的修改了,调Bug 改Bug,想想都头疼,以下就是一位对Bug很有经验的开发者总结出来的有关Android错误的解决方法。
android.os.build.serial 在API 9 中引入,如果低于9的系统可以通过这个方法获取这个信息值 java代码如下: public static String getDeviceSerial() { String serial = unknown; try { Class clazz = Class.for...