我有一台服务器,以JSON格式提供实时数据,该数据每秒更新一次。我必须在我的android应用中显示它。
我是一个初学者,我尝试通过线程更新异步任务,并每秒设置一个延迟。
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// Perform the HTTP request for data and process the response.
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
它用尽了内存并在一段时间后崩溃
有备用吗?
答案 0 :(得分:0)
尝试将代码放入处理程序线程
Runnable runnable = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(this, 1000);
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
};
// start it with:
mHandler.post(runnable);