android.os.NetworkOnMainThreadException异常
来源:互联网 发布:apk软件注册码破解 编辑:程序博客网 时间:2024/06/05 04:24
这个异常大概意思是在主线程访问网络时出的异常。 Android在4.0之前的版本 支持在主线程中访问网络,但是在4.0以后对这部分程序进行了优化,也就是说访问网络的代码不能写在主线程中了。
为了把域名转化为ip地址,我在主线程中调用GetInetAddress函数。
public static String GetInetAddress(String host){//域名host转化为ip地址IPAddress返回 String IPAddress = ""; InetAddress ReturnStr1 = null; try { ReturnStr1 = java.net.InetAddress.getByName(host); IPAddress = ReturnStr1.getHostAddress(); System.out.println(IPAddress); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); return IPAddress; } return IPAddress; }
结果l出错,log信息为:
01-15 09:16:50.340: E/AndroidRuntime(3927): FATAL EXCEPTION: main01-15 09:16:50.340: E/AndroidRuntime(3927): android.os.NetworkOnMainThreadException01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)01-15 09:16:50.340: E/AndroidRuntime(3927): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)01-15 09:16:50.340: E/AndroidRuntime(3927): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)01-15 09:16:50.340: E/AndroidRuntime(3927): at java.net.InetAddress.getByName(InetAddress.java:289)01-15 09:16:50.340: E/AndroidRuntime(3927): at com.csipsimple.ui.HspLaunch.GetInetAddress(HspLaunch.java:655)01-15 09:16:50.340: E/AndroidRuntime(3927): at com.csipsimple.ui.HspLaunch$LoginBtnListener.onClick(HspLaunch.java:264)01-15 09:16:50.340: E/AndroidRuntime(3927): at android.view.View.performClick(View.java:4084)01-15 09:16:50.340: E/AndroidRuntime(3927): at android.view.View$PerformClick.run(View.java:16966)01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.Handler.handleCallback(Handler.java:615)01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.Handler.dispatchMessage(Handler.java:92)01-15 09:16:50.340: E/AndroidRuntime(3927): at android.os.Looper.loop(Looper.java:137)01-15 09:16:50.340: E/AndroidRuntime(3927): at android.app.ActivityThread.main(ActivityThread.java:4746)01-15 09:16:50.340: E/AndroidRuntime(3927): at java.lang.reflect.Method.invokeNative(Native Method)01-15 09:16:50.340: E/AndroidRuntime(3927): at java.lang.reflect.Method.invoke(Method.java:511)01-15 09:16:50.340: E/AndroidRuntime(3927): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)01-15 09:16:50.340: E/AndroidRuntime(3927): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)01-15 09:16:50.340: E/AndroidRuntime(3927): at dalvik.system.NativeStart.main(Native Method)
解决方法有两个:
1、调用上面函数前,在onCreate函数里面添加如下代码:
//详见StrictMode文档StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
2、使用Thread、Runnable、Handler这三个类
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.hsp_login_dialog); new Thread(runnable).start();}Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String val = data.getString("value"); Log.i("mylog","转化后地址为-->" + val); }}Runnable runnable = new Runnable(){ @Override public void run() { // // TODO:在这里调用GetInetAddress函数处理,返回IPAddress. // Message msg = new Message(); Bundle data = new Bundle(); data.putString("value",IPAddress); msg.setData(data); handler.sendMessage(msg); }}
0 0
- android.os.NetworkOnMainThreadException异常
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- 异常:android.os.NetworkOnMainThreadException
- 异常android.os.NetworkOnMainThreadException
- 异常:android.os.NetworkOnMainThreadException
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- 异常:android.os.NetworkOnMainThreadException
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- android.os.NetworkOnMainThreadException异常
- android.os.NetworkOnMainThreadException异常
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- android.os.NetworkOnMainThreadException异常
- 【Cocos2d-x 2.2.2 自学笔记之一】利用自己的jar快捷创建项目
- Android中使用多进程的应用程序注意
- typedef,结构体,共用体,联合体
- 【Linux Kernel】抢占
- TKinter里面如何引入图片
- android.os.NetworkOnMainThreadException异常
- Linux 文件系统剖析
- 快速学习C语言一: Hello World
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- 【Linux kernel】中断
- SugarCRM安装与初探
- iphone 备忘录2--利用UIImageView实现动画特效------------cocos2d-x3.0正式版本(7.12)
- java子类中的构造函数
- 异常处理 【java笔记】