在安卓中连续获取URL数据



我想连续从URL获取JSON数据,并在Android页面中实时显示。我不知道该怎么做。

您可以使用一种称为轮询的技术(每 X 秒获取一次数据(,或者如果实时很重要,请使用 websockets。

private void timerfor10Seconds() {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 30 seconds
urlCall();
}
}, 10*1000);  // add timer for 10 sec
}
urlCall(){
call api
}
on api reponse update UI and recall timerfor10Seconds()
2. Option
Use Socket.Io library for continuous data broadcast, accordingly you need to implement the same library on your server side, as calling api continuously is not a correct way to display live data.