【发布时间】:2014-06-26 15:41:25
【问题描述】:
这是我的应用程序的一部分。 (您可以将下面的代码作为一个独立的应用程序运行)在该网站(url)上,使用php语言,从其他网站解析一些数字,并制作一个数组并将其编码为JSON数组,并显示。
但是,使用下面的代码(没有关闭功能!)ProgressDialog 出现在 doInBackground 之后。
当我像下面这样向 onPostExecute 添加解除函数时,它永远不会出现。但是当我为检查对话框窗口设置日志时,它说有一个对话框。
我听说 doInBackground 会冻结 UI,但它会在显示对话框之前冻结。
其他类似问题已经找到解决方案,从 mAsyncTask().execute().get() 中删除 .get(),但我的代码中没有任何 get()。
布尔变量 loadfinish 用于等待完成 asynctask,以在 asynctask 之后显示该网站的结果。如果我删除
while(loadFinish == false)
在那里,它自动出现和消失得很好,但我不能立即显示结果......
添加)我发现在doInBackground之后出现对话框需要几秒钟...为什么?!
Add2) 我曾尝试在new mAsyncTask().execute() 前后移动对话框代码,但它也不起作用...
public class MainActivity extends Activity {
boolean loadFinish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
//just a button for starting asynctask
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFinish = false;
new mAsyncTask().execute();
// get and make json array to java array
while (loadFinish == false)
;
}
});
// add array to Custom_List_Data, and set custom row_adapter to listView.
// if I delete while(loadFinish == false), array with initial data is added.
}
private class mAsyncTask extends AsyncTask<String, String, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("asdf");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
String url = "http://songdosiyak.url.ph/MouiRate/etoos/3/main/";
String response_str = "";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response_str = client.execute(request, responseHandler);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
loadFinish = true;
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
}
对不起,我的英语语言能力很差,感谢您的阅读!
【问题讨论】:
标签: android android-asynctask progressdialog