网络验证更新组件报NetworkOnMainThreadException和ViewRootImpl$CalledFromWrongThreadException
来源:互联网 发布:翼龙贷 网同城网络 编辑:程序博客网 时间:2024/05/02 11:41
今天试验写android获得web交互,仿照李兴华的web服务器交换数据。
添加了一些新的功能,即在EditText中输入用户名和密码,然后生成url,反馈给http,并解析获得数据。
实现时点击button,出现了android.os.NetworkOnMainThreadException这个错误,错误行定位在下面感叹号的地方。
public boolean connect(String username , String password) {// String username , String passwordtry { URL url = new URL("http", "192.168.1.102", 8080, "/test/android.jsp?id=" + username + "&password=" + password); //URL url = new URL("http", "192.168.1.102", 8080,//"/test/android.jsp?id=youjie&password=123");HttpURLConnection http = (HttpURLConnection) url.openConnection();byte data[] = new byte[512];//!!!!!注意这行,需要获得输入流时int len = http.getInputStream().read(data);if (len > 0) {// 构造一个字符串String str = new String(data, 0, len).trim();return Boolean.parseBoolean(str);}http.getInputStream().close();} catch (Exception e) {e.printStackTrace(); return false;}return false;}
查了一下网上有关这个的错误信息,说是访问网络不能在主线程中进行,心想行啊,起个runnable不就可以了么,然后点击按钮触发的事件时启动这个线程,然后得到反馈的数据更新ui。
可以问题又来了,报这个错误Only the original thread that created a view hierarchy can touch its views ,大概意思是只有原始线程才能修改其组建。
解决方法,大神的,来:http://blog.csdn.net/shenyuemei/article/details/11030679
采用了官方建议的方法,使用了runOnUiThread()这个方法,来更新ui。
public void startThread() {Runnable runable = new Runnable() {//调用connet放方法从http端获得数据@Overridepublic void run() {flag = connect(one.getText().toString(), two.getText().toString());//注意这句:使用系统的更新ui界面的线程MainActivity.this.runOnUiThread(updateThread);}};//更新UI界面的线程updateThread = new Runnable() {@Overridepublic void run() {tx.setText(String.valueOf(flag));}};//启动连接http端的线程new Thread(runable).start();}
ok,问题解决,具体项目代码:
0 0
- 网络验证更新组件报NetworkOnMainThreadException和ViewRootImpl$CalledFromWrongThreadException
- ViewRootImpl$CalledFromWrongThreadException
- android.view.ViewRootImpl$CalledFromWrongThreadException:
- android.view.ViewRootImpl$CalledFromWrongThreadException
- android.view.ViewRootImpl$CalledFromWrongThreadException
- android.view.ViewRootImpl$CalledFromWrongThreadException
- 异常:android.view.ViewRootImpl$CalledFromWrongThreadException
- android.view.ViewRootImpl$CalledFromWrongThreadException: UI线程和子线程(工作线程)冲突
- android.view.ViewRootImpl$CalledFromWrongThreadException异常处理
- .AppException: android.view.ViewRootImpl$CalledFromWrongThreadException: Only t
- android异常:android.view.ViewRootImpl$CalledFromWrongThreadException
- android.view.ViewRootImpl$CalledFromWrongThreadException错误处理
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Unhandled exception: android.view.ViewRootImpl$CalledFromWrongThreadException:
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Android JS调原生 有盟分享 打开微信分享 原程序崩溃问题 报android.view.ViewRootImpl$CalledFromWrongThreadException:
- Android 网络下载报networkonmainthreadexception异常
- Struts1.X 标签
- virtual 析构函数的作用
- veridata实验举例(5)修改主键上的列值,update操作会被拆分成两条语句
- C_PlusPlus学习笔记 - 4_C++程序结构 (C++语言程序设计【第三版】 郑莉等,清华大学出版社)
- 远程调试java程序
- 网络验证更新组件报NetworkOnMainThreadException和ViewRootImpl$CalledFromWrongThreadException
- 第3章 数组和字符串
- 算法复习(二)—— 最短路径
- RAC 客户端连接TNS
- Javascript中最常用的125个经典技巧
- 安装maven的全过程-已成功。
- ti dsp(tms320VC5502)+isp1581 usb2.0高速数据采集方案(3)
- Gradle编译极慢
- 胡思乱想