我有这个代码,并在MyAsyncOpenUrl我得到一个杰森和解析它来读取服务器消息。我将它存储在公共变量名称消息中。但我的问题是if语句在服务器消息到达之前正在运行。我如何等待任务,直到我收到消息? 由我读了in this question它似乎我可以使用get方法,但我不知道如何使用它? :(如何等待异步任务完成其工作?
message = "";
MyAsyncOpenUrl task1 = new MyAsyncOpenUrl("http://test.com/op.php");
try {
task1.execute(myURL).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//task1.onPostExecute(null);
if (message.equals("fail"))
Toast.makeText(getBaseContext(), "Operation faild!", Toast.LENGTH_LONG).show();
参考http://stackoverflow.com/questions/9671546/asynctask-android-example – Nikhil