android UI TextView setText显示服务器返回数据
来源:互联网 发布:python统计字母个数 编辑:程序博客网 时间:2024/05/01 17:17
android 的 ui是要在主线程中调用的,所以即使是返回的数据,要想在setText显示,也是要在主线程中写的。
1、在主方法里Handler handler = new Handler();
2、起一个线程,在线程里写setText,也就是接受服务器返回的json(我这里是json)
protected void subConnect(){
Thread thread = new Thread(new Runnable() {
public void run() {
final JSONObject jsonObject = subSever(); //返回的是json(服务器返回的)
handler.post(new Runnable() {
String txtString = null;
String message = null;
public void run() {
try {
message = jsonObject.getString("msg");
int phynum = jsonObject.getInt("phy");
int phytime = jsonObject.getInt("phytime");
physicalView.setText(String.valueOf(phynum));//获取json里的数据并显示
timeView.setText(String.valueOf(phytime));//获取json里的数据并显示
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(TextUtils.isEmpty(message))
{
txtString = "无数据";
}
Toast.makeText(getApplicationContext(), txtString, Toast.LENGTH_LONG).show();
}
});
}
});
thread.start();//启动线程
}
线程的生命周期
1、创建状态:new Thread
2、就绪状态:调用start()方法,等待cpu分配内存
3、执行状态:执行重写的run()方法
4、阻塞状态:有时候暂停的停止运行可能是cpu把空间分配给别的地方使用了
5、死亡状态:线程完成任务,自动销毁,结束
- android UI TextView setText显示服务器返回数据
- android textview settext 问题
- android.widget.TextView.setText()
- android TextView.setText
- android TextView类setText()方法显示数字
- Android中TextView调用setText无效不显示问题
- android-textview-settext()输出数字
- android textview settext卡顿深层次原因
- Android中TextView的SetText()方法注意事项
- Android TextView setText卡顿问题
- Android UI系列:TextView显示文本
- 安卓错误:android.widget.TextView.setText(TextView.java:3473)
- Android为textView设置setText的时候报错
- Android之TextView频繁调用setText的困惑
- (转)Android中TextView的SetText()方法注意事项
- Android中TextView中的.setText和.append的区别
- textview的settext
- TextView的setText方法
- 怎么用maven创建一个Java Web项目?
- PHP 浏览器禁用cookie,解决session变量不能传值
- TabHost
- Scroller的使用,让View随心所欲的移动
- HTML5多媒体audio和video(一)
- android UI TextView setText显示服务器返回数据
- RAM,ROM和Flash memory等存储器的比较
- 求连续子数组的最大和
- Wine虚拟技术及其使用
- JSON
- IOS-TextField详解
- dp 杂练/专练 round2
- solr5.5.0服务启动与停止
- 文件属性和管理