我正在运行Jsoup.connect()。获取Asynctask以防止主线程上的网络,但是由于get(),此方法也会阻止UI。有任何建议可以防止或最大限度地减少阻塞吗?
@Override
protected Document doInBackground(String... strings) {
Document doc = null;
try {
doc = Jsoup.connect(strings[0]).get();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
答案 0 :(得分:0)
根据您的评论
task.execute(url).get();
到
task.execute(url);
调用get
不再使其成为异步。它会等待结果阻塞ui线程。只需使用.execute
http://developer.android.com/reference/android/os/AsyncTask.html
public final Result get ()
Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.
Returns
The computed result.