我正在编写一些代码以发现Android中处理异步的不同方式,而且我已经完成了AsyncTask,现在我已经有了过去的未来,但是这一次它让我有点...在等待未来完成时未显示进度条
代码本身工作正常,除了(旋转)进度条只在下载操作完成后才显示的事实......尽管我告诉代码之前显示它...
下面的代码:
public void startDownload(final String url) {
bar.setVisibility(View.VISIBLE); // progress bar should appear here
ExecutorService pool = Executors.newCachedThreadPool();
Gson gson = new Gson();
Future<String> promise = pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while((s = buffer.readLine()) != null) response += s;
} catch (IOException ioe) {
ioe.printStackTrace();
}
return response;
}
});
while(promise.isDone() == false) {
// wait
}
bar.setVisibility(View.GONE); // and disappear here
try {
GitHubStatus status = gson.fromJson(promise.get(), GitHubStatus.class);
statusText.setText(status.getStatus());
bodyText.setText(status.getBody());
dateText.setText(status.getCreationDate());
} catch (ExecutionException ee) {
ee.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
// instead it briefly appears here, and then disappears again
}
对于那些不知道的人;期货执行自己,因此你不需要调用“运行”或“开始”方法来启动它
我试图把一个Log.v("DEBUG: ", "Loading...");
在等待它完成的循环中,它确实打印邮件就好,很多时候在那个
我没有试过循环,那也不行,而且我甚至有ProgressDialog
,不能正常工作,尝试...
我知道我无法在UI线程忙时更新UI线程,但我只在UI线程中的任何工作完成之前和之后设置了可见性,所以这里有什么问题?为什么它不工作?
请显示您的布局文件。 – arts777
我的布局文件在这里是无关紧要的,我知道progessbar出现在它应该在的地方,它只是不应该出现,当它应该 –
人们应该停止要求与被问的问题无关的东西。 – FWeigl