您需要使用AsynTask来执行此操作,请使用以下代码
class PerformTask extends AsyncTask<Void,Void,Void>{
ArrayList<String> info=new ArrayList<String>();
@Override
protected Void doInBackground(Void... voids) {
try
{
Document document = Jsoup.connect("https://nevakit.com/otobus/izmir/412").get();
Elements broadcasts = document.select("div[id=1]");
for (Element element : broadcasts) {
info.add(element.getElementsByTag("div").text());
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
String total=null;
for(String temp:info)
{
total+=temp;
}
txtView.setText(total);
}
}
从下面的例子中调用它
new PerformTask().execute();