在我的应用中,我通过离子库从网站获得了一些TEXT
,我使用progressBar
来显示下载该数据的进度。
但是我的主要问题是在onComplete
我实现了一些与我从网站上获得的TEXT
一起使用的方法,这些是一些循环方法,它们使用了很多东西和其他东西,实际上我的progressBar
在开始时工作正常,但是当它完整下载数据时,progressBar
只会坚持到onComplete()
完成所有方法。
我认为progressBar
无论如何都会运行,如果不从onComplete()
移除方法就不会卡住这种可能吗?
这是我使用我在onClick()
事件中调用的离子库的函数:
private void getHTML(){
progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
progressDialog.show();
Ion.with(getApplicationContext())
.load("IP")
.asString()
.setCallback(new FutureCallback<String>() {
@SuppressLint("SetTextI18n")
@Override
public void onCompleted(Exception e, String result) {
htmlresultart = result;
htmlresultart = htmlresultart.replace("</td>", "\n");
getTable();
getHeader();
getBody();
progressDialog.cancel();
}
});
}
答案 0 :(得分:1)
如果onCompleted()回调中调用的方法执行过多,则需要在后台线程上运行。在这种情况下,使用Ion进行回调是没有意义的,相反,您应该同步获取数据,然后执行所有其他任务,所有这些都在一个后台线程上,如下所示:
private void getHTML(){
progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
progressDialog.show();
// start a background thread
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
String htmlresultart = null;
try {
String htmlresultart = Ion.with(getApplicationContext())
.load("IP")
.asString()
.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
// if htmlresultart is null at this point, an exception occured and the string couldn't be fetched so you most likely should abort the following processing.
if (htmlresultart != null) {
htmlresultart = htmlresultart.replace("</td>", "\n");
getTable();
getHeader();
getBody();
progressDialog.cancel();
}
}
});
}